Magento 2 – Resource Name Not Defined

magento2modelresource-modeltable

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();

Best Answer

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();