2011-08-24 41 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的假設,因爲你既然你已經改變了你所有的別名和列名沒有列出您的版本