2014-01-09 22 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实体... –