0

I'm trying to follow Alan Storm's tutorial here.

The code works fine when I try to insert or retrieve a single record, but it cracks when I use collections. I followed all steps he illustrated in the tutorial, which drives me crazy because only collections wouldn't work!!

 public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Vendor\Mymodule\Model\ConfigurationsFactory $myClass
) {
    $this->myClass = $myClass;
    parent::__construct($context);
}

public function execute() {

     $todo = $this->myClass->create();

     $collection = $todo->getCollection();

     foreach($collection as $item)
      {
        var_dump('Item ID: ' . $item->getConfigId());
        var_dump($item->getData());
      }
    exit;

}

This gives the following error: Vendor\Mymodule\Model\Configurations does not extend \Magento\Framework\DataObject

The code for configurations model is as below

namespace Vendor\Mymodule\Model;
use Magento\Framework\Model\AbstractModel;

class Configurations extends AbstractModel
{
    protected function _construct()
    {
        $this->_init('Vendor\Mymodule\Model\ResourceModel\Configurations');
    }
}

ResourceModel

namespace Vendor\Mymodule\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Configurations extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('vendor_configurations','config_id');
    }
}

Collection

namespace Vendor\Mymodule\Model\ResourceModel\Configurations;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected function _construct()
    {
        $this->_init('
             Vendor\Mymodule\Model\Configurations',
            'Vendor\Mymodule\Model\ResourceModel\Configurations');
    }
}

any hints would be really appreciated.

Thanks

  • You can update the code lines of Configurations model? – Khoa TruongDinh Oct 11 '16 at 16:10
  • @KhoaTruongDinh code added – Digital fortress Oct 11 '16 at 16:52
1

I recently ran into this very same issue and spent close to 4 hours trying to figure out the root-cause.

Turns out that there was an extra space (i.e. an invisible character) at the start of the my Model class specification in my Collection.php file ( File Location: Gsd\Punchout\Model\ResourceModel\Vendor )

Before:

protected function _construct()
{
    $this->_init(' ***Gsd\Punchout\Model\Vendor***', 'Vendor\Module\Model\ResourceModel\Vendor');

}

After:

protected function _construct()
{
    $this->_init('***Gsd\Punchout\Model\Vendor***', 'Vendor\Module\Model\ResourceModel\MyModel');

}

Posting the "fix" here in the hopes that it will help someone in the future.

In the code-snippet you've posted above, I do see a new-line character (i.e. invisible character) in the Model argument.

Root Cause

Once you fix that erratta, clear your cache and try again. It should resolve the issue.

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.