0

I have my custom table and created respective models related to that. My issue is : When I try to get collection or set data into that table that time I am getting an error

Model collection resource name is not defined.

Please have a look in my files : Table name : custom_test_customertest

Namespace/Modulename/Model/Test/Customertest.php

namespace Namespace\Modulename\Model\Test;
use Magento\Framework\Model\AbstractModel;

class Customertest extends AbstractModel
{
    protected function _construct()
    {
      $this->_init('Namespace\Modulename\Model\ResourceModel\Test\Customertest');
    }
}

Namespace/Modulename/Model/ResourceModel/Test/Customertest.php

namespace Namespace\Modulename\Model\ResourceModel\Test;
class Customertest extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{  
    protected function _construct()
    {
        $this->_init('custom_test_customertest', 'id');
    }
}

Namespace/Modulename/Model/ResourceModel/Test/Customertest/Collection.php

namespace Namespace\Modulename\Model\ResourceModel\Test\Customertest;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

    protected function _construct()
    {
       $this->_init(
'Namespace\Modulename\Model\Test\Customertest','Namespace\Modulename\Model\ResourceModel\Test\Customertest'
      );
    }   
}

When I try to collect collection from phtml or anywhere I am getting issue:

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $objectManager->get('\Namespace\Modulename\Mode\Test\Customertest')->getCollection();
  • Have you cleared var/generation? – Prashant Valanda May 7 '16 at 8:06
  • @PrashantValanda yes – Krupali May 7 '16 at 8:31
1

You have simple typo error Mode to Model

 $objectManager->get('\Namespace\Modulename\Mode\Test\Customertest')->getCollection();

Chnage to:

 $objectManager->get('\Namespace\Modulename\Model\Test\Customertest')->getCollection();

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.