Closed
Description
Preconditions
- Magento 2.1.12
Steps to reproduce
- create a collection
- the following code will only addExpressionFieldToSelect before addFieldToSelect, and the 'stime_part' won't appear in the result.
$bookingCollection = $this->_bookingCollectionFactory->create();
$bookingCollection->addExpressionFieldToSelect('stime_part', 'time(start_time)', []);
$bookingCollection->addFieldToSelect('finish_time', 'finish_date');
Expected result
Field 'stime_part' populated with calculated data.
Actual result
Field 'stime_par' doesn't populated with calculated data.
The bug is in the following code
Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
protected function _initSelectFields()
{
$columns = $this->_select->getPart(\Magento\Framework\DB\Select::COLUMNS);
$columnsToSelect = [];
foreach ($columns as $columnEntry) {
list($correlationName, $column, $alias) = $columnEntry;
if ($correlationName !== 'main_table') {
// Add joined fields to select
if ($column instanceof \Zend_Db_Expr) {
$column = $column->__toString();
}
$key = $alias !== null ? $alias : $column;
$columnsToSelect[$key] = $columnEntry;
}
}
$columns = $columnsToSelect;
$columnsToSelect = array_keys($columnsToSelect);
if ($this->_fieldsToSelect !== null) {
$insertIndex = 0;
foreach ($this->_fieldsToSelect as $alias => $field) {
if (!is_string($alias)) {
$alias = null;
}
if ($field instanceof \Zend_Db_Expr) {
$column = $field->__toString();
} else {
$column = $field;
}
if ($alias !== null && in_array(
$alias,
$columnsToSelect
) ||
// If field already joined from another table
$alias === null && isset($alias, $columnsToSelect)
) {
continue;
}
$columnEntry = ['main_table', $field, $alias];
array_splice($columns, $insertIndex, 0, [$columnEntry]);
// Insert column
$insertIndex++;
}
} else {
array_unshift($columns, ['main_table', '*', null]);
}
$this->_select->setPart(\Magento\Framework\DB\Select::COLUMNS, $columns);
return $this;
}
This line skips the ExpressionField
if ($correlationName !== 'main_table') {
Working around is
Call all addFieldToSelect before addExpressionFieldToSelect
Metadata
Metadata
Assignees
Labels
The issue has been fixed in 2.2 release lineThe issue has been fixed in 2.3 release lineGate 2 Passed. Manual verification of the issue description passedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedGate 1 Passed. Automatic verification of issue format passedGate 4. Acknowledged. Issue is added to backlog and ready for developmentThe issue has been reproduced on latest 2.1 releaseThe issue has been reproduced on latest 2.2 releaseThe issue has been reproduced on latest 2.3 release