0

I have a model,resource model,collection file shown below.

app/code/Magge/Custom/Model/Categrory.php

namespace Magge\Custom\Model;

use Magento\Framework\Model\AbstractModel;

class Categrory extends AbstractModel
{
protected function _construct()
{
    $this->_init('Magge\Custom\Model\ResourceModel\Categrory');
    }
}

app/code/Magge/Custom/Model/ResourceModel/Categrory.php

namespace Magge\Custom\Model\ResourceModel;

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

class Categrory extends AbstractDb
{
protected function _construct()
{
    $this->_init('customer_products', 'customproduct_id');
}
}

app/code/Magge/Custom/Model/ResourceModel/Categrory/Collection.php

namespace Magge\Custom\Model\ResourceModel\Categrory;

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

class Collection extends AbstractCollection
{

protected function _construct()
{
    $this->_init('Magge\Custom\Model\Categrory', 'Morello\Customproducts\Model\ResourceModel\Categrory');
}

}

I have a another module named CustomPriceApi within which I have a price.php file in Model .From this file I am try use the factory class but got the error.

namespace Insigma\CustomPriceApi\Model;

class Price implements PriceInterface {

public function __construct(
\Magge\Custom\Model\CategoryFactory $factoryCategory,
.....
)
{
$this->factoryCategory   = $factoryCategory;
}

Within its public method I use

$category = $this->factoryCategory->create()->addFieldToFilter('price_id',array('eq' => $priceId))->getData();

And I get error.I tried using addAttributetoFilter() but still get undefined method.Please help me in this regard.

3 Answers 3

1

What you want to do is directly inject the collection factory related to your model into the constructor of the Price class.

So, in the constructor of Insigma\CustomPriceApi\Model\Price, inject the Magge\Custom\Model\ResourceModel\Categrory\CollectionFactory factory. Collection classes are what have the addFieldToFilter and other search related methods for retrieving results from a collection.

You will still need to call the create() method on the CollectionFactory before using and calling filtering methods to get results.

You can technically call getCollection() from a model, but it is a violation of Separation Of Concerns and considered a deprecated approach since a model should only be concerned with the data it represents.

1
  • Tnx, this was quite informative, I was missing the create() part.
    – Korbin
    Aug 13, 2022 at 9:33
0

Try below:

$category = $this->factoryCategory->create()->getCollection()->addFieldToFilter('price_id',array('eq' => $priceId))->getData();
2
  • Hi @Rutvee,I use it.Each time I use getCollection() I get varnish cache error. Sep 5, 2018 at 10:34
  • please check after clearing your generated folder and cache Sep 5, 2018 at 10:38
0

You did the mistake at collection class:

$this->_init('Magge\Custom\Model\Categrory', 'Morello\Customproducts\Model\ResourceModel\Categrory');

You have put wrong resource class at here

it should be

Magge\Custom\Model\ResourceModel\Category

from

'Morello\Customproducts\Model\ResourceModel\Categrory'

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.