Skip to content

Commit

Permalink
ENGCOM-6489: #24357 Eav sort order by attribute option_id #24360
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirZaets authored Dec 17, 2019
2 parents 8181457 + 2f9d297 commit eaa0617
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,13 @@ public function addValueSortToCollection($collection, $dir = \Magento\Framework\
$collection,
$attribute,
$valueExpr
)->addOptionToCollection(
$collection,
$attribute,
$valueExpr
);

$collection->getSelect()->order("{$attribute->getAttributeCode()}_value {$dir}");
$collection->getSelect()->order("{$attribute->getAttributeCode()}_order {$dir}");

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ public function addOptionValueToCollection($collection, $attribute, $valueExpr)
return $this;
}

/**
* Add Join with option for collection select
*
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
* @param \Magento\Eav\Model\Entity\Attribute $attribute
* @param \Zend_Db_Expr $valueExpr
* @return $this
*/
public function addOptionToCollection($collection, $attribute, $valueExpr)
{
$connection = $this->getConnection();
$attributeCode = $attribute->getAttributeCode();
$optionTable1 = $attributeCode . '_option_t1';
$tableJoinCond1 = "{$optionTable1}.option_id={$valueExpr}";
$valueExpr = $connection->getIfNullSql(
"{$optionTable1}.sort_order"
);

$collection->getSelect()->joinLeft(
[$optionTable1 => $this->getTable('eav_attribute_option')],
$tableJoinCond1,
["{$attributeCode}_order" => $valueExpr]
);

return $this;
}

/**
* Retrieve Select for update Flat data
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ public function testAddValueSortToCollection()
$attrOption->expects($this->once())->method('addOptionValueToCollection')
->with($collection, $this->abstractAttributeMock, $expr)
->willReturnSelf();
$select->expects($this->once())->method('order')->with("{$attributeCode}_value {$dir}");
$attrOption->expects($this->once())->method('addOptionToCollection')
->with($collection, $this->abstractAttributeMock, $expr)
->willReturnSelf();
$select->expects($this->once())->method('order')->with("{$attributeCode}_order {$dir}");

$this->assertEquals($this->model, $this->model->addValueSortToCollection($collection, $dir));
}
Expand Down

0 comments on commit eaa0617

Please sign in to comment.