在Magento目录页面上,有一个附加到集合的连接。联接完美。
$products->joinTable(
array('as_name' => 'some_table'),
'product_id=entity_id',
array('some_var' => 'variable'),
array('store_id' => array('eq' => '1')),
'inner'
);
如果我要运行查询raw,some_var
列将具有正确的值。此外,如果我添加ORDER BY some_var DESC
raw,它将正确排序。但是,如果我使用Magento的$products->setOrder('some_var', 'desc');
,Magento提交的查询将变为:
ORDER BY `e`.`some_var` DESC
如何让Magento不添加"e"
? some_var不是该选择的一部分,应该是as_name
。
答案 0 :(得分:2)
想出来:
$collection->getSelect()->order('some_var DESC');
答案 1 :(得分:1)
如果您为连接列指定的别名实际上与基表中的列相同,则可能会遇到问题。
addAttributeToSort()首先检查_joinFields中的列,如果找到了它(在你的情况下会是这样),它会调用_getAttributeFieldName()将别名解析为完全限定的列引用。 _getAttributeFieldName()在检入_joinFields之前检查_staticFields中的别名。这意味着如果您有冲突,基表列将获胜。
public function addAttributeToSort($attribute, $dir='asc')
{
if (isset($this->_joinFields[$attribute])) {
$this->getSelect()->order($this->_getAttributeFieldName($attribute).' '.$dir);
return $this;
}
if (isset($this->_staticFields[$attribute])) {
$this->getSelect()->order("e.{$attribute} {$dir}");
}
if (isset($this->_joinAttributes[$attribute])) {
$attrInstance = $this->_joinAttributes[$attribute]['attribute'];
$entityField = $this->_getAttributeTableAlias($attribute).'.'.$attrInstance->getAttributeCode();
} else {
$attrInstance = $this->getEntity()->getAttribute($attribute);
$entityField = 'e.'.$attribute;
}
if ($attrInstance) {
if ($attrInstance->getBackend()->isStatic()) {
$this->getSelect()->order($entityField.' '.$dir);
} else {
$this->_addAttributeJoin($attribute, 'left');
if (isset($this->_joinAttributes[$attribute])) {
$this->getSelect()->order($attribute.' '.$dir);
} else {
$this->getSelect()->order($this->_getAttributeTableAlias($attribute).'.value '.$dir);
}
}
}
return $this;
}
protected function _getAttributeFieldName($attributeCode)
{
if (isset($this->_joinAttributes[$attributeCode]['condition_alias'])) {
return $this->_joinAttributes[$attributeCode]['condition_alias'];
}
if (isset($this->_staticFields[$attributeCode])) {
return sprintf('e.%s', $attributeCode);
}
if (isset($this->_joinFields[$attributeCode])) {
$attr = $this->_joinFields[$attributeCode];
return $attr['table'] ? $attr['table'] .'.'.$attr['field'] : $attr['field'];
}
$attribute = $this->getAttribute($attributeCode);
if (!$attribute) {
throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid attribute name: %s.', $attributeCode));
}
if ($attribute->isStatic()) {
if (isset($this->_joinAttributes[$attributeCode])) {
$fieldName = $this->_getAttributeTableAlias($attributeCode).'.'.$attributeCode;
} else {
$fieldName = 'e.'.$attributeCode;
}
} else {
$fieldName = $this->_getAttributeTableAlias($attributeCode).'.value';
}
return $fieldName;
}
NB :所有这些都是基于Magento 1.5的假设,因为你没有列出你的版本