Skip to content

SearchCriteriaBuilder builds wrong criteria (ORDER BY part) #5738

Closed
@flancer64

Description

@flancer64

Empty SearchCriteria causes the error in \Magento\Framework\Data\Collection\AbstractDb::_setOrder
Target SQL has wrong ORDER BY clause (without any field inside):

SELECT ... ORDER BY  DESC

This is the code to get error:

class SalesRules
{
    /** @var \Magento\Framework\Api\Search\SearchCriteriaBuilder */
    protected $_builderSearchCriteria;
    /** @var \Magento\SalesRule\Api\RuleRepositoryInterface */
    protected $_repoRule;

    public function __construct(
        \Magento\Framework\Api\Search\SearchCriteriaBuilder $builderSearchCriteria,
        \Magento\SalesRule\Api\RuleRepositoryInterface $repoRule

    ) {
        $this->_builderSearchCriteria = $builderSearchCriteria;
        $this->_repoRule = $repoRule;
    }

    /**
     * Add sales rules.
     */
    public function init()
    {
        $crit = $this->_builderSearchCriteria->create();
        $all = $this->_repoRule->getList($crit);
    }
}

Method \Magento\SalesRule\Model\RuleRepository::getList adds order for $filed=null:
mage2_set_order_getlist

I suppose we need field validation in \Magento\Framework\Data\Collection\AbstractDb::_setOrder

private function _setOrder($field, $direction, $unshift = false)
{
    if($field) {
        ...
    }
    return $this;
}

or validation in the \Magento\SalesRule\Model\RuleRepository::getList:

    /** @var \Magento\Framework\Api\SortOrder $sortOrder */
    foreach ($sortOrders as $sortOrder) {
        $field = $sortOrder->getField();
        if($field) {
            $collection->addOrder(
                $field,
                ($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
            );
        }
    }

Metadata

Metadata

Assignees

Labels

Fixed 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 not validGate 1 Failed. Automatic verification of issue format is failedReproduced 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 release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions