I have a custom eav structure and there is store_id save in array format in the database field.
I need to filer collection with addFieldToFilter with store_id.
$collection = $this->xyzCollectionFactory->create();
$collection->addFieldToFilter('store_id', ['in' => $this->storeManager->getStore()->getId()]);
Datastore in the database below format
["1","2","8","3","4","5"]
this filter is not working, anyone has an idea how can I do that?
3 Answers 3
Reference Link :- https://fishpig.co.uk/magento/tutorials/addattributetofilter/
Try This :
$storeId[] = $this->storeManager->getStore()->getId();
addAttributeToFilter('store_id', array('in' => array($storeId)));
-
its not workingMage2 Developer– Mage2 Developer2019年07月08日 08:13:36 +00:00Commented Jul 8, 2019 at 8:13
-
please explain briefly what type of data you want and which format of dataRonak Rathod– Ronak Rathod2019年07月08日 08:15:27 +00:00Commented Jul 8, 2019 at 8:15
-
Its quite clear, i need to match value of field, which field value store in array format in database.Mage2 Developer– Mage2 Developer2019年07月08日 08:19:51 +00:00Commented Jul 8, 2019 at 8:19
-
you mean data store in array format ?Ronak Rathod– Ronak Rathod2019年07月08日 08:21:43 +00:00Commented Jul 8, 2019 at 8:21
-
please attach example of your data formatRonak Rathod– Ronak Rathod2019年07月08日 08:22:13 +00:00Commented Jul 8, 2019 at 8:22
First of all, check that store_id field create in your custom table or not. If yes, then make sure that you extend Magento\Eav\Model\Entity\Collection\AbstractCollection class in your collection file.
For more reference : click here
Try this code regarding my friend RK
$storeIds = array();
$storeIds[] = 1; //store id
$collection = $this->xyzCollectionFactory->create();
$collection->addFieldToFilter('store_id', ['in' => (array)$storeIds]);
Or
$storeId = 1;
$collection->getSelect()->where('main_table.store_id in (?)', [0, $storeId]);