2011-08-24 48 views
0

在Magento目录页面上,有一个加入到集合中。连接完美。Magento内部加入和订购

$products->joinTable(
    array('as_name' => 'some_table'), 
    'product_id=entity_id', 
    array('some_var' => 'variable'), 
    array('store_id' => array('eq' => '1')), 
    'inner' 
); 

如果我运行原始查询,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

,这是一个有点难以肯定。 –

回答

1

想通了: $collection->getSelect()->order('some_var DESC');

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的假设,因为你既然你已经改变了你所有的别名和列名没有列出您的版本