Please could someone advise why I am getting this error when calling my code below:

Uncaught Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Category\Flat::validate()

namespace exampleVendor\exampleModule\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
    protected $categoryFactory;
    protected $categoryRepository;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryFactory,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
    ) {
        $this->categoryFactory = $categoryFactory;
        $this->categoryRepository = $categoryRepository;

    public function execute()
        $categories = $this->categoryFactory->create()
            ->addAttributeToFilter('is_active', 1)

        foreach ($categories as $category){

As requested the full error message is:

Uncaught Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Category\Flat::validate() in /vendor/magento/module-catalog/Model/Category.php:1077 
    Stack trace: #0 /generated/code/Magento/Catalog/Model/Category/Interceptor.php(570): Magento\Catalog\Model\Category->validate() 
    #1 /vendor/magento/module-catalog/Model/CategoryRepository.php(194): Magento\Catalog\Model\Category\Interceptor->validate() 
    #2 /vendor/magento/module-catalog/Model/CategoryRepository.php(112): Magento\Catalog\Model\CategoryRepository->validateCategory(Object(Magento\Catalog\Model\Category\Interceptor)) 
    #3 /generated/code/Magento/Catalog/Model/CategoryRepository/Interceptor.php(24): Magento\Catalog\Model\CategoryRepository->save(Object(Magento\Catalog\Model\Category\Interceptor)) 
    #4 /app/code/exampleVendor/exampleModule/Controller in /vendor/magento/module-catalog/Model/Category.php on line 1077
  • Can you try to run this code in adminhtml scope? – Jonathan Ribas May 13 '18 at 8:55
  • How do you do that? – Goose84 May 13 '18 at 8:58
  • Can you post whole error message here ? – Keyur Patel May 13 '18 at 9:12
  • I have updated the question to have the full error – Goose84 May 13 '18 at 9:17
  • Instead of $this->categoryRepository->save($category); can't you use $category->save(); – Keyur Patel May 13 '18 at 9:39

1/ You must change the scope inside your custom module routes.xml

<?xml version="1.0" encoding="utf-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> <router id="admin"> <route id="yourRouteId" frontName="yourRouteUrl"> <module name="yourModulename" /> </route> </router> </config>

2/ Your controller will now extend backend : class Index extends \Magento\Backend\App\Action

3/ Run this page from your admin, you must be logged in inside your Magento back office.

  • Thanks that works now though I have a different issue not related to the above issue so I will create a new question – Goose84 May 13 '18 at 10:27
  • is it updating category by id? – jafar pinjar Sep 10 '18 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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