Open Mordreak opened 3 years ago
Wrote a test to reproduce the issue:
/**
* @magentoDataFixture Magento/Cms/_files/block.php
* @magentoDataFixture Magento/Cms/_files/blocks.php
*/
public function testFilterJoinedFieldonFlatTableCollection(): void
{
$processor = new class() extends AbstractGridSourceProcessor implements HyvaGridCollectionProcessorInterface {
/**
* @param CmsBlockCollection $source
* @param string $gridName
*/
public function afterInitSelect(AbstractDbCollection $source, string $gridName): void
{
$select = $source->getSelect();
// add field from joined table
$source->getSelect()->joinLeft(
'cms_block_store',
'main_table.block_id = cms_block_store.block_id',
['test_field' => 'cms_block_store.store_id']
);
}
};
$args = [
'gridName' => 'test',
'processors' => [$processor],
'sourceConfiguration' => ['collection' => CmsBlockCollection::class],
];
/** @var CollectionGridSourceType $sut */
$sut = ObjectManager::getInstance()->create(CollectionGridSourceType::class, $args);
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
$searchCriteriaBuilder->addFilter('test_field', '1');
$rawGridData = $sut->fetchData($searchCriteriaBuilder->create());
$records = $sut->extractRecords($rawGridData);
$this->assertGreaterThan(0, count($records));
}
Considerations:
Navigation::getSearchCriteria
view model method, but dismissed the idea because it would expose the view model to the implementation details of the underlying grid source type.HyvaGridSourceProcessor::beforeLoad
method in \Hyva\Admin\Model\GridSourceType\CollectionGridSourceType::fetchData
before the search criteria is applied to the collection. Dismissed this idea because mutating the search criteria would cause multiple loads.\Hyva\Admin\Model\GridSourceType\CollectionGridSourceType::fetchData
, but instead of using a processor, hardcode the mapping of aliases to real column names for flat table collections. This would probably involve cloning the search criteria after the processor beforeLoad
methods where called to avoid duplicate loading.
Steps to reproduce :
/!\ I only tried this with a text field, but I guess this is the same for any other column type.
If I remember well, in the magento 1 grid builder we had to add a "filter_index" attribute on the column definition to let the grid know that it must filter with the real "table.column_name" instead of the alias. Maybe it could be a solution