Skip to content

Exception #0 (InvalidArgumentException): ModelItem does not extend MagentoFrameworkDataObject

I ma trying to render data from database to a custom block, but i am getting error as the below screenshot
I’ve review the code multiple times and still con not catch the issue
I am using Magento ver. 2.4.4

enter image description here

the code as the following snapshot

Model file => src/app/code/Mastering/SampleModule/Model/Item.php

<?php 

namespace MasteringSampleModelModel;
use MagentoFrameworkModelAbstractModel;

class Item extends AbstractModel{
    protected function _construct(){
        $this->_init(MasteringSampleModuleModelResourceModelItem::class);
    }
}

ResourceModel file => src/app/code/Mastering/SampleModule/Model/ResourceModel/Item.php

<?php 

namespace MasteringSampleModuleModelResourceModel;
use MagentoFrameworkModelResourceModelDbAbstractDb;

class Item extends AbstractDb{
    
    public function _construct(){
        $this->_init('mastering_sample_item', 'id');
    }
    
}

Collection file => src/app/code/Mastering/SampleModule/Model/ResourceModel/Item/Collection.php

<?php

namespace MasteringSampleModuleModelResourceModelItem;

use MagentoFrameworkModelResourceModelDbCollectionAbstractCollection;


class Collection extends AbstractCollection{
    protected $_idFieldName = 'id';

    public function _construct(){
        $this->_init(
            MasteringSampleModuleModelItem::class,
            MasteringSampleModuleModelResourceModelItem::class
        );
    }
}

Block file => src/app/code/Mastering/SampleModule/Block/Article.php

<?php

namespace MasteringSampleModuleBlock;

use MagentoFrameworkViewElementTemplate;
use MasteringSampleModuleModelResourceModelItemCollection;
class Article extends Template
{
    /**
     * @var Collection
     */
    private $collection;

    /**
     * Display constructor.
     * @param TemplateContext $context
     * @param Collection $collection
     * @param array $data
     */
    public function __construct(
        TemplateContext $context,
        Collection $collection,
        array $data = []
    ) {
        $this->collection = $collection;
        parent::__construct($context, $data);
    }

    /**
     * @return Collection
     */
    public function getItems()
    {
        return $this->collection;
    }
}
?>