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.
cms
block
model with store data. It should work this way, right? – Luke Apr 24 '17 at 20:10