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()));