【问题标题】:magento: join product with attribute valuemagento:加入具有属性值的产品
【发布时间】:2017-04-06 18:08:16
【问题描述】:

我正在尝试从 php 脚本中列出我的一些产品,我需要获取一些属性的值。例如,我正在尝试这样的事情::

Mage::app();
Mage::app()->setCurrentStore(1);
$collection = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect('computer_type')
   ->joinAttribute('computer_type_value', 'eav/entity_attribute_option', "computer_type", null, 'left', 0)

"computer_type" 来自产品集合商店的一个 ID,该 ID 位于 eav_attribute_option_value 表的 option_id 字段中:

 +----------+-----------+----------+--------+
 | value_id | option_id | store_id |  value |
 +----------+-----------+----------+--------+
 |     3738 |        14 |        0 | server |
 +----------+-----------+----------+--------+

我想将这个表与我的集合结果结合起来,以便显示值“server”而不是 id“3738”,但我这样做的方式不起作用,尽管我进行了所有搜索。

我没有找到如何仅在一个查询中实现这一目标。有可能吗?

【问题讨论】:

  • 您如何使用数据? Magento 使用属性模型的“前端”渲染器从前端模板访问选择属性值的特定方法。如果这是你正在做的,那么你应该效仿。如果是用于数据馈送或其他/自定义逻辑,则继续:D
  • @RobbieAverill:感谢您的评论,确实它是在前端实例之外使用的,这就是我使用纯 php 库的原因

标签: sql magento collections product


【解决方案1】:

请使用以下代码加入产品属性:

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$collection = Mage::getModel('catalog/product')
         ->getCollection()
         ->addAttributeToSelect('*')
     ->addAttributeToFilter('computer_type',array('neq'=>0))
     ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
         ->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

【讨论】:

  • 感谢您的提议,我可能会以错误的方式使用它,但它实际上是否与包含属性值的表相结合,我只从目录/产品中获得'computer_type',就像我已经拥有的一样。
猜你喜欢
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多