2011-12-08 40 views
1

基本上,我希望鏈接sales/order和sales/order_invoice表,以便在通過發票循環時從sales/order獲取order_source feild;magento加入訂單和發票

這是我attmpt:

$collection = Mage::getModel('sales/order_invoice')->getCollection(); 
$collection = $collection->addAttributeToFilter('order_id', array('from' =>$from)); 
$collection->joinAttribute('order_source', 'order/order_source', 'order_id', null, 'left'); 

foreach($collection as $invoice){ 

$orderSource = $invoice->getOrderSource(); 

//do other stuff 

} 

我是新來的Magento

+0

order_source是我添加的客戶屬性 – rosh3000

+0

'joinAttribute'對我來說看起來不錯。你能解釋什麼是錯的嗎? – clockworkgeek

+0

@clockworkgeek我無法訪問該變量,即它始終是'null',當我嘗試排序時發現問題,因爲它給了我一個錯誤消息:'致命錯誤:... Main_table.order_source未找到... (你說的沒錯,joinAttribute') – rosh3000

回答

3

(技術上不是一個正確的答案做的只是另一種方式)

我,而不是使用下面的join什麼我用過:

$collection->join(array('order' => 'order'), 'order.entity_id=order_id', array('order_source'=>'order_source'), null , 'left'); 

$collection->addAttributeToSort('order_source', 'DESC'); 

foreach($collection as $invoice){ 

$orderSource = $invoice->getOrderSource(); 

//do other stuff 

} 

希望它可以幫助別人

PS獲得所有屬性從訂單,你可以使用下列內容:(但不推薦,因爲*優化):

$collection->join(array('order' => 'order'), 'order.entity_id=order_id', array('order'=>'*'), null , 'left'); 

,並獲得具體領域使用:

$collection->join(array('order' => 'order'), 'order.entity_id=order_id', array('some_feild'=>'some_feild', 'some_other_feild' => 'some_alies_for_other_feild'), null , 'left'); 

和然後使用$collection->getSomeAliesForOtherFeild()$collection->getSomeFeild()

相關問題