3

I made a custom CRUD Module and when i call the collection without the addFieldToFilter function everything works fine but when i try to filter the collection i get the exception

1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Vendor\Module\Model\AgencyProduct::addFieldToFilter

namespace Zweygart\Agency\Block;

/**
 * Class Stock
 * @package Zweygart\Agency\Block
 */
class Stock extends \Magento\Framework\View\Element\Template {

    protected $agencyDataFactory;
    protected $agencyProductFactory;
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Zweygart\Agency\Model\AgencyDataFactory $agencyDataFactory,
        \Zweygart\Agency\Model\AgencyProductFactory $agencyProductFactory
    ) {
        $this->agencyDataFactory = $agencyDataFactory;
        $this->agencyProductFactory = $agencyProductFactory;

        parent::__construct($context);
    }

    function getAgencysWithInventory() {

        $productCollection = $this->agencyProductFactory
            ->create();
            //->addFieldToFilter('zw_agency_product_id', 1);
        foreach($productCollection as $product) {
            die("Gnarf");
        }

    }

}
4

Try to get collection following way:

$productCollection = $this->agencyProductFactory
            ->create()->getCollection()->addFieldToFilter('zw_agency_product_id', 1);
  • Oh man my falt thx so much. Didn't see the missing get collection. – Cenzo Aug 24 '16 at 6:19

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.