I need to get and add a new Core table data. So, I tried based on get custom table data way(Model, ResourceModel) but it's not working for me.

For example I need to collect customer_group data

What I did: (Using Api)

        namespace Vendor\Module\Controller\Index;
        use Magento\Checkout\Model\Cart as CustomerCart;
        use Magento\Customer\Model\Session;
        use Magento\Customer\Api\Data\GroupInterfaceFactory;
        use Magento\Customer\Api\Data\GroupInterface;
        use Magento\Customer\Api\GroupRepositoryInterface;

        class Test extends \Magento\Framework\App\Action\Action

            protected  $_groupDataFactory;

            protected $dataObjectProcessor;

            public function __construct(
                \Magento\Framework\App\Action\Context $context,
                Session $csession,
                \Psr\Log\LoggerInterface $logger,
                \Magento\Framework\Registry $coreRegistry,
                GroupRepositoryInterface $groupRepository,
                GroupInterfaceFactory $groupDataFactory,
                \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor

                $this->dataObjectProcessor = $dataObjectProcessor;

            public function execute()

                         $customerGroup = $this->_groupDataFactory->create();

                        catch (\Exception $e)
                           // echo $e;


Model Way:

           protected $_groupFactory;

            public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Customer\Model\Group $groupFactory


            public function getTableData()




Both above way not working for me , Suggest me How to get and add a data to core table.

1 Answer 1


You injected the right class which is Magento\Customer\Api\GroupRepositoryInterface but you didn't use it.

To get the list of customer groups you can call:


PS: don't forget to assign this class to a class variable of yours such as protected $groupRepository if you want this code to work

To create new customer groups you first need to use Magento\Customer\Api\Data\GroupInterfaceFactory to create the customer group:

$customerGroup = $this->groupDataFactory->create();

NB: please replace the code and tax class id with what you need

And then you can save it using the group repository:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.