Skip to main content
10 votes

How to get product collections by set of ids in magento 2?

Try below code: $this->getCollection()->addFieldToFilter('entity_id', array( 'in' => array(3,8,9,207)) );
Arunendra's user avatar
  • 7,458
7 votes

How to get product collections by set of ids in magento 2?

<?php namespace Mageplaza\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_productCollectionFactory; public function __construct( ...
Savoo's user avatar
  • 487
6 votes

How to get product collections by set of ids in magento 2?

Yeah you can do the following: $this->getCollection()->addFieldToFilter('entity_id',['in' => [...]]);
Raphael at Digital Pianism's user avatar
6 votes
Accepted

using sql queries is good practice in magento 2

SQL queries are not recommended in Magento official Documentaion. You can get a result in fastly while executing SQL queries, but it affects security. Avoid raw SQL queries Raw SQL queries can lead ...
Arunprabakaran M's user avatar
4 votes

using sql queries is good practice in magento 2

kindly look into it once it will be helpful https://devdocs.magento.com/guides/v2.3/ext-best-practices/extension-coding/security-performance-data-bp.html
mohith's user avatar
  • 496
3 votes

How to get product collections by set of ids in magento 2?

If you want get the collection which keep its ids order that is not ASC or DESC, you could use codes like below: $ids = '4,5,3,1,2'; $collection = $this->collectionFactory->create() -&...
Key Shang's user avatar
  • 3,425
3 votes
Accepted

How do I specify unique constraint for multiple columns in InstallSchema.php?

Problem solved: in the script, I directly executed the request: $myRequest = "ALTER TABLE..."; $installer->getConnection()->query($myRequest ); EDIT: finally found how to do it with the ...
enrico69's user avatar
  • 515
3 votes
Accepted

What methods typically go in Model, Resource and Collection

First of all, there is a method loadByEmail in the customer model (or Magento 2) that is just a proxy to the one in the resource model. As a general idea (but this does not happen always), the ...
Marius's user avatar
  • 198k
3 votes

How to resolve the "Direct object instantiation (object of \Zend_Db_Expr) is discouraged in Magento 2"?

Not sure about this, but I think you can use factories for the zend classes also. Try this: Have this member in your class: private $zendExprFactory; And add this in your constructor: public ...
Marius's user avatar
  • 198k
2 votes

Get the average price for product collection

I would do a loop through each of the products, have a count and a sum and then work out the average. When your looping through your products remember to have a second loop to check if it is a ...
Gaz Smith's user avatar
  • 109
2 votes

How to resolve the "Direct object instantiation (object of \Zend_Db_Expr) is discouraged in Magento 2"?

Instead of using Zend_Db_Expr, you can use \Magento\Framework\DB\Sql\Expression Like use Magento\Framework\DB\Sql\Expression; class Yourclass { public function test() { .... $Qry = $connection->...
Amit Bera's user avatar
  • 77.6k
1 vote
Accepted

Magento 1.9.x ORM query to get past successful orders with specific billing street, city, state and zip

Note that the address components are not indexed in the DB, so the following query will scan all the success orders in the address table. $collection = Mage::getResourceSingleton('sales/...
kiatng's user avatar
  • 694
1 vote
Accepted

Magento\Framework\Exception\NoSuchEntityException when updating products

Use below code: <?php use Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = ...
Prashant Valanda's user avatar
1 vote
Accepted

Get order data by product id Magento 1.9

$productId = 12735; $collection = Mage::getResourceModel('sales/order_item_collection') ->addAttributeToFilter('product_id', array('eq' => $productId)) ->load(); $order_ids = $...
Pradeep Sanku's user avatar
1 vote
Accepted

Magento orm model and direct SQL query

There is no problem about using direct SQL queries, if you are 100% sure which database tables you'd read or update. Most common issue here would be missing some tables to update, leading to data ...
Raul Sanchez's user avatar
  • 3,125

Only top scored, non community-wiki answers of a minimum length are eligible