4

I'm creating a custom module to make a model by using mage2gen, I named this model critic:

app/code/Vendor/Module/Model/Critic.php

namespace Vendor\Module\Model;

use Vendor\Module\Api\Data\CriticInterface;
use Vendor\Module\Api\Data\CriticInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;

/**
 * Class Critic
 *
 * @package Vendor\Module\Model
 */
class Critic extends \Magento\Framework\Model\AbstractModel
{

    protected $dataObjectHelper;

    protected $_eventPrefix = 'vendor_module_critic';
    protected $criticDataFactory;


    /**
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param CriticInterfaceFactory $criticDataFactory
     * @param DataObjectHelper $dataObjectHelper
     * @param \Vendor\Module\Model\ResourceModel\Critic $resource
     * @param \Vendor\Module\Model\ResourceModel\Critic\Collection $resourceCollection
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        CriticInterfaceFactory $criticDataFactory,
        DataObjectHelper $dataObjectHelper,
        \Vendor\Module\Model\ResourceModel\Critic $resource,
        \Vendor\Module\Model\ResourceModel\Critic\Collection $resourceCollection,
        array $data = []
    ) {
        $this->criticDataFactory = $criticDataFactory;
        $this->dataObjectHelper = $dataObjectHelper;
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }

    /**
     * Retrieve critic model with critic data
     * @return CriticInterface
     */
    public function getDataModel()
    {
        $criticData = $this->getData();

        $criticDataObject = $this->criticDataFactory->create();
        $this->dataObjectHelper->populateWithArray(
            $criticDataObject,
            $criticData,
            CriticInterface::class
        );

        return $criticDataObject;
    }
}

app/code/Vendor/Module/Model/ResourceModel/Critic.php

namespace Vendor\Module\Model\ResourceModel;

    /**
     * Class Critic
     *
     * @package Vendor\Module\Model\ResourceModel
     */
    class Critic extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
    {

        /**
         * Define resource model
         *
         * @return void
         */
        protected function _construct()
        {
            $this->_init('vendor_module_critic', 'critic_id');
        }
    }

app/code/Vendor/Module/Model/ResourceModel/Critic/Collection.php

namespace Vendor\Module\Model\ResourceModel\Critic;

 /**
 * Class Collection
 *
 * @package Vendor\Module\Model\ResourceModel\Critic
 */
 class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
 {

    /**
     * @var string
     */
    protected $_idFieldName = 'critic_id';

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init(
            \Vendor\Module\Model\Critic::class,
            \Vendor\Module\Model\ResourceModel\Critic::class
        );
    }
}

when i tried to get the data collection like this:

public function __construct(
 \Vendor\Module\Model\CriticFactory $criticFactory
){
  $this->criticFactory = $criticFactory;
}

public function execute(){
   $this->criticFactory->create()->getCollection()->addFieldToFilter('critic_id','1');
}

I always got an error

Call to a member function addFieldToFilter() on null

then I tried to print the getCollection, but it returns empty

print_r($this->criticFactory->create()->getCollection());

then I tried to get the type, then it returns null

print_r(gettype($this->criticFactory->create()->getCollection()));
3
  • Please refer to this answer : magento.stackexchange.com/questions/176646/… Mar 2 '20 at 4:01
  • Your code seems all correct. It may be related to file permission issue. Mage2Gen has some issues with file permission when you extract the zip. Please check and correct your file ownership and permissions. Run compilation and check. Mar 2 '20 at 4:49
  • @JaiminSutariya just checked my ownership and permission , run compilation etc still same error
    – jojo
    Mar 2 '20 at 4:53
0

please try to load the collection by ResourceModel like instead of this

\Vendor\Module\Model\CriticFactory $criticFactory

Use this

\Vendor\Module\Model\ResourceModel\Critic\CollectionFactory $criticFactory

then run the setup:di:compile and check.

4
  • still the same error
    – jojo
    Mar 2 '20 at 4:44
  • 1
    Could you please try to load the collection by ResourceModel like instead of this \Vendor\Module\Model\CriticFactory $criticFactory Use this \Vendor\Module\Model\ResourceModel\Critic\CollectionFactory $criticFactory Mar 2 '20 at 4:54
  • it works, but how cant i use the factory like in Magento 2 previous version ? how can i add new data using this factory?
    – jojo
    Mar 2 '20 at 9:45
  • @jojo, I have updated my answer using my above comment. also, please accept the answer and upvote, So it will help others. Mar 2 '20 at 11:43

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.