如何在 magento 中按标签名称过滤标签/product_collection资源模型


How to filter tag/product_collection resource model by tag name in magento

我想按标签名称过滤标签/product_collection资源模型。

为此,我在下面编写了代码

$collection = Mage::getResourceModel('tag/product_collection');
$collection->addFieldToFilter("name",array('like'=>'%dixit%'));
print_r($collection->getData());

然后它只显示空数组。

如果我发表评论

$collection->addFieldToFilter("name",array('like'=>'%dixit%'));

此行然后显示下面的输出

Array ( [0] => Array ( [entity_id] => 323 [entity_type_id] => 4 [attribute_set_id] => 4 [type_id] => simple [sku] => 8018-90 [has_options] => 0 [required_options] => 0 [created_at] => 2010-03-11 12:17:46 [updated_at] => 2013-07-24 12:12:56 [product_id] => 323 [item_store_id] => 1 [tag_id] => 1 [name] => dixit [tag_status] => 0 [tag_name] => dixit ) ) 

那么如何使用类似查询此模型进行过滤。

我尝试了两种过滤器属性方式

$collection->addFieldToFilter("name",array('like'=>'%dixit%'));
$collection->addAttributeToFilter("name",array('like'=>'%dixit%'));

但是它们都不起作用。

    $collection->getSelect()->Where(' name like ?',"% dixit %");

使用这种方式,您可以根据需要过滤tag_name。

getSelect() 方法获取选择查询,我们只需附加 where 查询以使用 where 方法选择查询。

它的方法很简单。我已经检查了它的工作正常。

您好,检查下面的代码可能会对您有所帮助

 $tagName='dixit';
$tagId= Mage::getModel('tag/tag')->loadByName($tagName)->getId();
$tagId = 3;  
$products = Mage::getResourceModel('tag/product_collection')
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('name')
                ->addTagFilter($tagId);
print_r($products->getData());