0

I'm trying to get data from my custom table and show it to frontend.

I followed to this tutorial: https://www.emiprotechnologies.com/technical_notes/magento-technical-notes-60/post/how-to-create-model-resource-model-and-collection-in-magento-2-400

Here is my code:

app/code/HaoNMN/Museinc/Model/SampleTable.php:

<?php
namespace HaoNMN\Museinc\Model;

use Magento\Framework\Model\AbstractModel;

class SampleTable extends AbstractModel
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
        $this->_init('HaoNMN\Museinc\Model\ResourceModel\SampleTable');
    }
}

app/code/HaoNMN/Museinc/Model/ResourseModel/SampleTable.php:

<?php
namespace HaoNMN\Museinc\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class SampleTable extends AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('museinc_sampletable', 'id');
    }
}

app/code/HaoNMN/Museinc/Model/ResourseModel/SampleTable/Collection.php:

<?php
namespace HaoNMN\Museinc\Model\ResourceModel\SampleTable;

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

class Collection extends AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'HaoNMN\Museinc\Model\SampleTable',
            'HaoNMN\Museinc\Model\ResourceModel\SampleTable'
        );
    }
}

app/code/HaoNMN/Museinc/Controller/Index/Index.php:

<?php
namespace HaoNMN\Museinc\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use HaoNMN\Museinc\Model\SampleFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_modelFactory;

    /**
     * @param Context $context
     * @param NewsFactory $modelNewsFactory
     */
    public function __construct(
        Context $context,
        SampleFactory $modelFactory
    ) {
        parent::__construct($context);
        $this->_modelFactory = $modelFactory;
    }

    public function execute()
    {
        $sampleModel = $this->_modelFactory->create();

        $item = $sampleModel->load(1);
        var_dump($item->getData());

        $data = $sampleModel->getCollection();
        var_dump($data->getData());
    }
}

And final is my route:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="museinc" frontName="museinc">
            <module name="HaoNMN_Museinc"/>
        </route>
    </router>
</config> 

But when I access my url: http://localhost/magentoCE233/museinc/index/index, it got error like this:

enter image description here

I search some post in here and someone said that this is solved by run command php bin/magento setup:di:compile. But when I run, it showed this error:

enter image description here

Can you tell me what wrong did I do?

5
  • Can you double check that when you reference ResourseModel in the file names you actually mean ResourceModel? Commented Jan 8, 2020 at 9:42
  • Try use HaoNMN\Museinc\Model\SampleTableFactory; in your controller and check it. Commented Jan 8, 2020 at 10:39
  • I fixed in controller but still got error: Fatal error: Uncaught Error: Class 'HaoNMN\Museinc\Model\ResourceModel\SampleTable' not found in C:\xampp\htdocs\magentoCE233\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php:116 Stack trace: #0
    – Tomato
    Commented Jan 8, 2020 at 10:44
  • Check your ResourceModel folder name. You write in question is ResourseModel. Commented Jan 8, 2020 at 10:52
  • I agree with @DhadukMitesh you should check your folder name
    – Mohit Rane
    Commented Jan 8, 2020 at 11:00

3 Answers 3

0

Your Model class is SampleTable and you're adding SampleFactory in construct. I don't know if that's a typo error but it should be SampleTableFactory.

After Changes Run these commands

sudo chmod -R 777 var/ pub/ generated/
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:flush
sudo chmod -R 777 var/ pub/ generated/

That should resolve the problem.

7
  • I fixed like you said. I change SampleFactory to SampleTableFactory, and check permission also. Then my command setup:di:compile run ok, but when I access my url, it's still got error Fatal error: Uncaught Error: Class 'HaoNMN\Museinc\Model\ResourceModel\SampleTable' not found in C:\xampp\htdocs\magentoCE233\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php:116 Stack trace: #0
    – Tomato
    Commented Jan 8, 2020 at 10:33
  • can you show your di.xml?
    – Mohit Rane
    Commented Jan 8, 2020 at 10:39
  • It's so long. I will give you a google drive link instead: drive.google.com/open?id=1Lzob2bYh6u8bIzp6kO0HC5kvn6EepG07
    – Tomato
    Commented Jan 8, 2020 at 10:43
  • I just want to make sure that you have defined the ResourceModel collection to di.xml
    – Mohit Rane
    Commented Jan 8, 2020 at 10:45
  • How should I define it?
    – Tomato
    Commented Jan 8, 2020 at 10:47
0

Have you check owner permission ?

4
  • Yes, it's full control
    – Tomato
    Commented Jan 8, 2020 at 10:31
  • This is not an answer. Please use this in a comment. Commented Jan 8, 2020 at 10:54
  • He doesn't have enough reputation to comment @DhadukMitesh
    – Mohit Rane
    Commented Jan 8, 2020 at 10:57
  • But this is not an answer. And community disallow this. Commented Jan 8, 2020 at 10:58
0

The error says clearly that your directory permission are not right.

For a quick fix just run:chmod 777 -R $pathToYourFolderWithMagentoProject

But I don't recommend it cus it's not a good practice, just do it for a quick check. You could learn about file permissions, it's not that hard.

For a detailed guide please refer how to set the file permissions from the official guide.

https://devdocs.magento.com/guides/v2.3/install-gde/prereq/file-system-perms.html

Btw

Each time you change something in the constructor:

 public function __construct(
    EntityFactoryInterface $entityFactory,
    Logger $logger,
    FetchStrategyInterface $fetchStrategy,
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null
) {
    parent::__construct($entityFactory);
    $this->_fetchStrategy = $fetchStrategy;
    if ($connection !== null) {
        $this->setConnection($connection);
    }
    $this->_logger = $logger;
}

You need to clear the related files under the directory:

var/generated

Because Magento 2 generates the code and puts it there. This is because of dependency injection.

If you are running the app in the developer mode then the deletion of the files under var/generated is enough.

Alternatively you can use this commands:

php bin/magento setup:upgrade

Setup upgrade does wipe out everything from the var/generated and some additional directories.

or use

php bin/magento s:d:c

this will recompile all your classes :)

Hope this helps you understand a bit :)

Ps. Folder Naming is not correct, please fix the naming

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.