0

我需要一种方法来定位我的多个属性的 Magento 对象。我可以使用“loadByAttribute”方法通过单个参数查找对象,如下所示。

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

但是,我无法让它适用于多个参数。例如,我希望能够使用以下所有搜索参数进行上述查询。它可能如下所示。

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

是的,我知道您不需要所有这些字段来查找单个类别记录。但是,我正在编写一个模块来导出和导入整个网站,并且我需要在创建之前测试目标系统上是否已经存在一个对象,例如类别。为此,我必须检查是否已经存在具有多个匹配属性的相同类型的对象,即使它的 ID 不同。

4

1 回答 1

3

这可能无法回答您的问题,但可能会解决您的问题。
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.
对于可以同时使用它们的销售实体(订单、发票、贷项凭证和发货)有一个特殊情况。

于 2014-01-09T15:32:07.933 回答