bluvertigo
10/22/2018 - 1:02 PM

Model con query sql

<?
declare(strict_types=1);

use Magento\Sales\Model\Order\Item;
use Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory as ItemCollectionFactory;

class Example
{

    /**
     * @var ItemCollectionFactory
     */
    private $itemCollectionFactory;

    public function __construct(
        ItemCollectionFactory $itemCollectionFactory
    ) {

        $this->itemCollectionFactory = $itemCollectionFactory;
    }

    /**
     * Restituisce un array di order items da attivare
     * @return Item[]
     */
    public function execute(): array
    {
        $collection = $this->itemCollectionFactory->create();

        $collection->getSelect()
            ->distinct(true)
            ->joinInner(
                'tab1',
                'parent_id = order_id',
                ''
            )->joinLeft(
                'tabella2',
                'order_item_id = entity_id'
            )->where('activation_id IS NULL AND gl_activable = 1');

        return $collection->getItems();
    }
}