0

How to Create product collection with filter based on entity_id in Magento 2?

I tried below:

$model = $this->_objectManager->create('Vendor\MOdule\Model\Queue');
$ProductId = $this->skuProcessor->getNewSku($rowSku)['entity_id'];
$getId = $tagmodel->getCollection()
    ->addFieldToFilter('entity_id', $ProductId);

// Tried this too
/*  
   $getId=$model->getCollection()->create()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('entity_id',$ProductId);   */
if ($getId = '') {
    $tagmodel->setEntityId($ProductId);
    $tagmodel->save();
}

I am not getting the collection! nor the id is saved

My id should no be saved if already present in DB.

7ochem
  • 7,532
  • 14
  • 51
  • 80
Sushivam
  • 2,629
  • 3
  • 36
  • 88

1 Answers1

0

If the entity_id is key of the model, try this:

$model = $this->_objectManager->create('Vendor\MOdule\Model\Queue');
$ProductId = $this->skuProcessor->getNewSku($rowSku)['entity_id'];
$getId = $tagmodel->load($ProductId);

if (!$getId->getId()) {
    $tagmodel->setEntityId($ProductId);
    $tagmodel->save();
}
Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
  • Siarhey, i m trying to add only those product ids which are updated via csv import. For eg i have 2 product ids 1 and 2... i updated only one id and when i upload only that id which i updated should be saved in table – Sushivam Aug 05 '16 at 11:11
  • I get the ids correctly if i log $ProductId , but saving is the issue! – Sushivam Aug 05 '16 at 11:12
  • Better not use the objectManager this way, check out: http://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-directly – Anna Völkl Aug 05 '16 at 11:15
  • @AnnaVölkl I know, but this is the source code from the question – Siarhey Uchukhlebau Aug 05 '16 at 11:16
  • @SachinS Could you update the question with full code? – Siarhey Uchukhlebau Aug 05 '16 at 11:18
  • Siarhey, i have updated the code with full scenario here...http://magento.stackexchange.com/questions/129890/update-behaviour-in-csv-not-working-as-expected – Sushivam Aug 05 '16 at 11:23
  • @SachinS to help the quality of this platform, don't cross-reference questions and answers. This will make it hard for future readers encountering the same problem. If the code is needed to answer this question here, please update the question as well. – Anna Völkl Aug 05 '16 at 11:34
  • Ok sure... @Anna – Sushivam Aug 05 '16 at 11:43
  • @Siarhey, any update from my code, cause i still i dont get product collection :( – Sushivam Aug 08 '16 at 04:55