cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a join to collection without crashing MySQL

Adding a join to collection without crashing MySQL

Right from the start, I'm not sure I'm approaching this problem the right way.

 

I'm trying to populate an adminhtml grid with data from two joined tables, table `orders` and table `orderitems'. My approach to this thus far has been to use a class extending \Magento\Ui\DataProvider\AbstractDataProvider and override the getData() function in order to do a join on a collection. The problem is the join keeps crashing MySQL.

 

DataProvider class:

 

<?php

namespace OrderSystem\Model\Invoice;

use OrderSystem\Model\ResourceModel\Order\CollectionFactory;

/**
 * Class DataProvider
 */
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    /**
     * @var OrderSystem\Model\ResourceModel\Order\Collection
     */
    protected $collection;

    /**
     * @var array
     */
    protected $loadedData;

    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $pageCollectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $pageCollectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $pageCollectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->meta = $this->prepareMeta($this->meta);
    }

    /**
     * Prepares Meta
     *
     * @param array $meta
     * @return array
     */
    public function prepareMeta(array $meta)
    {
        return $meta;
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        
        $joinConditions = 'main_table.order_num = orderItem.order_num';
        $this->collection->joinTable(
            ['orderItem' => $this->collection->getTable('ordersystem_orderitems')],
            $joinConditions,
            ['order_num' => 'order_num']
        );
// we never get past this point without crashing...irrelevant code chunk removed       

        return $this->loadedData;
    }
}

I can include my di, template, and layout files if necessary, but they don't seem relevant at the moment.