Magento SQL查询:获取“单独不可见”的所有简单产品

时间:2012-12-13 14:36:57

标签: sql magento magento-1.7 entity-attribute-value

我希望直接在Magento数据库上编写一个SQL查询,该数据库获取所有Simple产品的可见性属性为“单独不可见”,其值为1我相信。

任何人都可以帮忙吗?到目前为止,我发现可见性值在表

中设置
  

catalog_product_entity_int

但一直无法进一步发展。感谢

2 个答案:

答案 0 :(得分:4)

如果没有ORM,这样做的原因有很多,所有这些都可能(或可能不)适用于您的需求(存储过滤器,从正确的表中读取数据等)。至少,您可以使用产品集合对象来构建您将运行的查询:

$coll = Mage::getModel('catalog/product')->getCollection();
$coll->addAttributeToFilter('visibility' , Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
echo $coll->getSelect();

生成的查询将如下所示:

SELECT `e`.*, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default`
    ON (`at_visibility_default`.`entity_id` = `e`.`entity_id`)
    AND (`at_visibility_default`.`attribute_id` = '526')
    AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`)
    AND (`at_visibility`.`attribute_id` = '526')
    AND (`at_visibility`.`store_id` = 1)
WHERE (IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) = '1')

答案 1 :(得分:1)

我希望这可能无法测试。

$sql = "SELECT * FROM catalog_product_entity_int WHERE visibility=1";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
foreach ($connection->fetchAll($sql) as $arraytest) {
    echo $arraytest['name'];
}