Как использовать цепочку команд в Magento 2? - PullRequest
0 голосов
/ 27 октября 2018

У меня есть пользовательский метод в одной коллекции, который нужно вызывать из другой модели. Смотрите ниже код:

Приложение / кода / Имя_вендор / MODULENAME / модель / Категория

namespace VendorName\ModuleName\Model;

use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use VendorName\ModuleName\Model\Items;
use Magento\Framework\DataObject\IdentityInterface;

class Categories extends AbstractModel implements IdentityInterface {

    protected $_items;

    public function __construct(
            Context $context, 
            Items $items, 
            AbstractResource $resource = null, 
            AbstractDb $resourceCollection = null, 
            array $data = []) {
        $this->_items = $items;
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }

    protected function _construct() {
        $this->_init('VendorName\ModuleName\Model\ResourceModel\Categories');
    }   

    public function getItemsCollection() {
        $itemsCollection = $this->_items->getCollection()->addCategoryFilter($this);
        return $itemsCollection;
    }
}

Приложение / код / ​​Имя_вендора / ModuleName / Model / ResourceModel / Items

namespace VendorName\ModuleName\Model\ResourceModel\Items;

use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;

class Collection extends AbstractCollection {

    protected $_modelStoreManagerInterface; 
    protected $_idFieldName = 'items_id';   

    public function __construct(
            EntityFactoryInterface $entityFactory, 
            LoggerInterface $logger, 
            FetchStrategyInterface $fetchStrategy, 
            ManagerInterface $eventManager, 
            StoreManagerInterface $modelStoreManagerInterface, 
            AdapterInterface $connection = null, 
            AbstractDb $resource = null) {
        $this->_modelStoreManagerInterface = $modelStoreManagerInterface;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
    }

    protected function _construct() {
        $this->_init('VendorName\ModuleName\Model\Items','VendorName\ModuleName\Model\ResourceModel\Items');
    }   

    public function addCategoryFilter($category) {
        if ($category instanceof VendorName\ModuleName\Model\Categories) {
            $category = array($category->getId());
        }        
        $this->getSelect()
        ->join(
            array('ci' => $this->getTable('vendorname_modulename_categories_items')),
            'main_table.items_id = ci.items_id',
            array ()
            )
        ->where('ci.categories_id in (?)', array(0, $category))
        ->group('main_table.items_id');       
        return $this;
    }
}

Я получаю сообщение об ошибке «Вызов неопределенного метода addCategoryFilter» в строке:

$itemsCollection = $this->_items->getCollection()->addCategoryFilter($this);

Похоже, цепочка команд не работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...