Skip to content

addExpressionFieldToSelect has to be called after all addFieldToSelect #17635

Closed
@dbsdsun

Description

@dbsdsun

Preconditions

  1. Magento 2.1.12

Steps to reproduce

  1. create a collection
  2. 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

No one assigned

    Labels

    Component: Framework/ModelFixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions