1

Background story:

I was trying to load a model with model_store relationship using this snippet. I failed to do that with my own model. Model is being loaded without store data. Some debuging later I found out that my table_store isn't even joined to main table in queries.

Same problem with a stock model:

I tried same actions with a stock model - \Magento\Cms\Model\Block. Results are the same. I can load an array of blocks but without any store data whatsoever.

Adding a store filter results with a zend error in sql generated by the ORM:

Exception #0 (Zend_Db_Statement_Exception): SQLSTATE[42S22]: Column not found: 
1054 Unknown column 'store_table.store_id' in 'where clause', 
query was: 
SELECT `main_table`.* FROM `cms_block` AS `main_table` WHERE ((`store_table`.`store_id` IN(0, 1, 2, 3, 4, 5))) ORDER BY main_table.block_id ASC

I used following code in a plugin to perform the tests:

<?php
namespace Vendor\Module\Plugin\Checkout\Model\Checkout;

use \Magento\Framework\Api\SearchCriteriaInterface;
use \Magento\Framework\Api\Search\FilterGroup;
use \Magento\Framework\Api\FilterBuilder;

class LayoutProcessor
{
    /**
     * @var SearchCriteriaInterface
     */
    private $criteria;

    /**
     * @var FilterGroup
     */
    private $filterGroup;

    /**
     * @var FilterBuilder
     */
    private $filterBuilder;

    /**
     * LayoutProcessor constructor for DI.
     * @param SearchCriteriaInterface $criteria
     * @param FilterGroup $filterGroup
     * @param FilterBuilder $filterBuilder
     */
    public function __construct(
        SearchCriteriaInterface $criteria,
        FilterGroup $filterGroup,
        FilterBuilder $filterBuilder,
        \Magento\Cms\Model\BlockRepository $blockRepository
    ){
        $this->searchCriteria = $criteria;
        $this->filterGroup = $filterGroup;
        $this->filterBuilder = $filterBuilder;

        $this->filterGroup->setFilters([
            $this->filterBuilder->setField('store')
                ->setConditionType('in')
                ->setValue([0,1,2,3,4,5])
                ->create()
        ]);
        $this->searchCriteria->setFilterGroups([$this->filterGroup]);
        $blocks = $blockRepository->getList($this->searchCriteria)->getItems();
        $firstBlock = $blocks[0];
    }
}

How can I load models with all data from related tables via repository? Currently I'm able to load them correctly only using collection.

  • Please provide additional info: What have you done to support your scheme beyond adding the table itself? What do you expect to cause Magento to know to join it? – Ryan Hoerr Apr 24 '17 at 18:42
  • @RyanHoerr let's ignore my table for now and try to load stock cms block model with store data. It should work this way, right? – Luke Apr 24 '17 at 20:10
  • Only if some bit of code tells Magento to load the two together. Tables and relationships don't happen by magic. If nothing in the collection or repository is doing that join, then it's not going to be there. – Ryan Hoerr Apr 24 '17 at 23:45
  • Could you help me then load magento/cms/model/block with store table joined using repository, not collection? – Luke Apr 25 '17 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.