2014-01-09 21 views
0

我需要一種方法來定位Magento對象的多個屬性。我可以使用'loadByAttribute'方法通過單個參數查找對象,如下所示。Magento:具有多個參數的資源模型'loadByAttribute'

$mageObj->loadByAttribute('name', 'Test Category'); 

但是,我一直無法得到這個工作的多個參數。例如,我希望能夠使用以下所有搜索參數執行上述查詢。它可能看起來像下面這樣。

$mageObj->loadByAttribute(array('entity_id' => 128, 
           'parent_id' => 1, 
           'name' => 'Test Category')); 

是的,我知道你不需要所有這些字段來找到一個單一的類別記錄。但是,我正在編寫一個模塊來導出和導入整個網站,並且在創建目標系統之前,我需要測試一個對象(如類別)是否已存在。要做到這一點,我必須檢查是否存在具有多個匹配屬性的相同類型的對象,即使它的ID不同。

回答

2

這可能不會回答你的問題,但它可能會解決你的問題。
Magento不支持多個屬性的loadByAttribute,但是可以這樣做。

$collection = $mageObj->getCollection() 
    ->addAttributeToFilter('entity_id', 128) 
    ->addAttributeToFilter('parent_id', 1) 
    ->addAttributeToFilter('name', 'Test Category'); 
$item = $collection->getFirstItem(); 
if ($item->getId()){ 
    //the item exists 
} 
else { 
    //the item does not exist 
} 

addAttributeToFilter適用於EAV實體(產品,類別,客戶)。 對於平面實體使用addFieldToFilter
銷售實體(訂單,發票,信用卡和貨件)有一個特殊情況可以使用它們。

+0

謝謝。有沒有辦法通過代碼告訴哪些實體是EAV,哪些是理智的? –

+0

'通過代碼告訴'是什麼意思? – Marius

+1

有沒有這樣的事情作爲一個理智的Magento實體... –