Come scrivere OR/e interrogare in magento2.?

Aug 17 2020

SELEZIONA main_table. sku, main_table. order_id, main_table. parent_item_id, main_table. name, main_table. product_idFROM sales_order_itemAS main_tableWHERE ( order_id= '3') AND ( parent_item_idIS NULL) AND ( ( skuCOME 'WSH06%') OR ( skuCOME 'MS10%') OR ( skuCOME 'MT08%') )

$sku = ['WSH06','MS10','MT08'];
$this->itemF ->create()->getCollection()
                       ->addAttributeToSelect('sku')
                       ->addAttributeToSelect('order_id')
                       ->addAttributeToSelect('parent_item_id')
                       ->addAttributeToSelect('name')
                       ->addAttributeToSelect('product_id')
                       ->addAttributeToFilter('order_id', array('eq' => $order_id))
                      ->addAttributeToFilter('parent_item_id', array('null' => true));
foreach ($sku as $key => $value) {
    $collectionData->addFieldToFilter('sku',array('like' => $value.'%'));
}

Risposte

1 NiravPatel Aug 17 2020 at 18:02

se vuoi fare o condizionare nella collezione devi seguire i passaggi seguenti.

$sku = ['WSH06','MS10','MT08'];
$this->itemF ->create()->getCollection()
                       ->addAttributeToSelect('sku')
                       ->addAttributeToSelect('order_id')
                       ->addAttributeToSelect('parent_item_id')
                       ->addAttributeToSelect('name')
                       ->addAttributeToSelect('product_id')
                       ->addAttributeToFilter('order_id', array('eq' => $order_id))
                      ->addAttributeToFilter('parent_item_id', array('null' => true));

$likeField = [];
$likeValue = []
foreach ($sku as $key => $value) {
    $likeField[] = 'sku';
    $likeValue[] = array('like' => $value.'%');
}

$collectionData->addFieldToFilter($likeField, $likeValue);
echo $collectionData->getSelect()->__toString();

è necessario passare l'argomento dell'array in addFieldToFilter per applicare o condizionare.

spero che questo funzioni per te!

Grazie e saluti, Nirav Patel