7

I want to filter collection in my custom model by product id to load the filter data value in admin grid.

My Model:- Vender\Modulename\Model\Outofstock

namespace Vender\Modulename\Model;

class Outofstock extends \Magento\Framework\Model\AbstractModel
{
    protected function _construct()
    {
        $this->_init('Vender\Modulename\Model\ResourceModel\Outofstock');
    }
}

Resource Model:-

namespace Vender\Modulename\Model\ResourceModel;

class Outofstock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected function _construct()
    {
        $this->_init('custom_table', 'id');
    }
}

Collection:-

namespace Vender\Modulename\Model\ResourceModel\Outofstock;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected $_idFieldName = 'id';

    protected function _construct()
    {
        $this->_init(
            'Vender\Modulename\Model\Outofstock',
            'Vender\Modulename\Model\ResourceModel\Outofstock'
        );
    }
}

I have already created a field with product_id and value 14

Please suggest me.If any other way is available to filter the collection before load in admin grid.

Can any one help me on this problem ? Any references or suggestions are highly appreciated.

2 Answers 2

1

You can try the below sample code.

If you define grid collection thought layout than you can use updater to add default filter.

<argument name="dataSource" xsi:type="object">
    Namespace\Modulename\Model\Resource\News\Collection
    <updater>Namespace\Modulename\Model\Resource\News\Collection\Updater</updater>
</argument>

and

<?php
namespace Namespace\Modulename\Model\Resource\News\Collection;

class CollectionUpdater implements \Magento\Framework\View\Layout\Argument\UpdaterInterface
{

    public function update($argument)
    {
        $argument->addFieldToFilter('you_field', 'value');

        return $argument;
    }
}
7
  • Hi Thanks for your answer but it not working Jun 27, 2017 at 13:05
  • @KamleshJha - Did you check the collection query?
    – Jjo
    Jun 27, 2017 at 13:16
  • @KamleshJha - please check the updated code above
    – Jjo
    Jun 27, 2017 at 13:25
  • Hi both the code are not working. Jun 27, 2017 at 13:38
  • @KamleshJha Hi, please check the updated code above.
    – Jjo
    Jun 27, 2017 at 13:48
0

did you ever get it solved?

The way you have build your model, resourceModel and collection seems to be fine but you did not post your code with which you tried to filter your custom collection.

I ran into problems trying to filter my custom collection within a (foreach)loop today and thought I'd post my code here to show where I got stuck and what the right way to filter is if you need to filter multiple times.

I have a table, a model, a resourceModel and a collection pretty much like yours. My custom model is called Items (there fore I need to use ItemsFactory, see note below) and I collect the manufacturer data in manufacturerData. In my class CustomAction (extending from \Magento\Framework\App\Action\Action) my construct funcion looks like this:

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Vendor\MyCustomModule\Model\ItemsFactory $itemsFactory, // this is my factory
    \Magento\Eav\Model\Entity\Attribute $entityAttribute
)
{
    $this->_itemsFactory = $itemsFactory;
    $this->_entityAttribute = $entityAttribute;
    return parent::__construct($context);
}

I needed to filter my custom collection on a custom attribute (or column name if you like) within a foreach loop and this is where I got stuck:

public function execute()
{ ... 
  $items = $this->_itemsFactory->create();
  $collection = $items->getCollection(); // my custom collection
  foreach($manufacturerData->getOptions() as $option) {
   ... // code to get what I need from manufacturer data and find it in my custom_attribute column in my custom collection:
       $item = $collection->addFieldToFilter('custom_attribute', $manufacturerAttributeId);
       var_dump($item->getData());
  }...

To my surprise it would always print the same item (the first item in the collection that was successfully found by the function). It turns out that I should not reuse the collection but simply create and (at the same time) filter WITHIN the foreach-loop:

foreach($manufacturerData->getOptions() as $option) {
   ... // code to get what I need from manufacturer data and find it in my custom_attribute column in my custom collection: 
   $item = $this->_itemsFactory->create()->getCollection()->addFieldToFilter('custom_attribute', $manufacturerAttributeId);
   var_dump($item->getData());
}

Now I got the right data filtered from my custom collection!

A note on custom collections and -Factory: My items collection has namespace and extends from:

namespace Vendor\MyCustomModule\Model\ResourceModel\Items;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

So there fore this generates a "factory" in generated/code/vendor_name/module_name/Model/class_model_name_Factory.php so my collection factory (since my model class is called "Items") would be ItemsFactory as you can see in the above __construct function where I import \Vendor\MyCustomModule\Model\ItemsFactory $itemsFactory.

So for filtering your custom Collection using addFieldToFilter with your custom column name don't forget to use class_model_name_Factory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.