0

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 Mastering\SampleModel\Model;
use Magento\Framework\Model\AbstractModel;

class Item extends AbstractModel{
    protected function _construct(){
        $this->_init(\Mastering\SampleModule\Model\ResourceModel\Item::class);
    }
}

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

<?php 

namespace Mastering\SampleModule\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

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 Mastering\SampleModule\Model\ResourceModel\Item;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;


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

    public function _construct(){
        $this->_init(
            \Mastering\SampleModule\Model\Item::class,
            \Mastering\SampleModule\Model\ResourceModel\Item::class
        );
    }
}

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

<?php

namespace Mastering\SampleModule\Block;

use \Magento\Framework\View\Element\Template;
use Mastering\SampleModule\Model\ResourceModel\Item\Collection;
class Article extends Template
{
    /**
     * @var Collection
     */
    private $collection;

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

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

1 Answer 1

0

src/app/code/Mastering/SampleModule/Model/Item.php

Incorrect

namespace Mastering\SampleModel\Model;

Correct

namespace Mastering\SampleModule\Model;

Also I recommend to add following into your etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Mastering\SampleModule\Model\Item">
        <arguments>
            <argument name="resource" xsi:type="object">Mastering\SampleModule\Model\ResourceModel\Item</argument>
            <argument name="resourceCollection" xsi:type="object">Mastering\SampleModule\Model\ResourceModel\Item\Collection</argument>
        </arguments>
    </type>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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