0

I am experimenting with Magento 2 and trying to add some filtering in my collection. Hence i am calling the SearchCriteriaBuilder in the constructor.

namespace MyCode\MyBlog\Model\Resource\Post;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;

class Collection extends \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection {
    protected $entityFactory;

    protected $logger;

    protected $fetchStrategy;

    protected $_eventManager = null;

    protected $_resource;

    private $searchCriteriaBuilder;


      public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        $connection = null,
        \Magento\Framework\Model\Resource\Db\AbstractDb $resource = null,
        SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);

        $this->searchCriteriaBuilder=$searchCriteriaBuilder;           
    }

but this drops me an error. Missing required argument $connection of MyCode\MyBlog\Model\Resource\Post\Collection.

When i remove the SearchCriteriaBuilder $searchCriteriaBuilder param from the constructor works fine

Any idea why this happens?

2

Required parameters should be before optional. Change constructor to

public function __construct(
    \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
    \Magento\Framework\Event\ManagerInterface $eventManager,
     SearchCriteriaBuilder $searchCriteriaBuilder,
    $connection = null,
    \Magento\Framework\Model\Resource\Db\AbstractDb $resource = null
)
  • Thanks for that. Seems to work I suppose that optional parameters are considered those that have null value. Right? *You missed a comma after the $searchCriteriaBuilder – Sakis Nov 10 '15 at 15:37

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.