Php magento中的concate sql查询
我是马根托的新手。我正在编写一个代码,如:Php magento中的concate sql查询,php,mysql,database,magento,Php,Mysql,Database,Magento,我是马根托的新手。我正在编写一个代码,如: $products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('*') ->addAttributeToSort("entity_id", "DESC") ->addAttributeToSelect(array('na
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToSort("entity_id", "DESC")
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setVisibility ( Mage::getSingleton( 'catalog/product_visibility')->getVisibleInSiteIds())
->setOrder($this->get_order(), $this->get_order_dir());
if($size != ''){
$products->getSelect()
->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
->joinLeft(array('cpf'=>'catalog_product_flat_1'),'cpf.entity_id=e.entity_id',array('shhirt_size'))
->where('at_int.value=30 AND cpf.shhirt_size='.$size.' AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ')
;
}else if($size == ''){
$products->getSelect()
->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
->where('at_int.value=30 AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ' )
;
}
在使用condition时,通过使用$products的值,我编写了如下代码:
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToSort("entity_id", "DESC")
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setVisibility ( Mage::getSingleton( 'catalog/product_visibility')->getVisibleInSiteIds())
->setOrder($this->get_order(), $this->get_order_dir());
if($size != ''){
$products->getSelect()
->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
->joinLeft(array('cpf'=>'catalog_product_flat_1'),'cpf.entity_id=e.entity_id',array('shhirt_size'))
->where('at_int.value=30 AND cpf.shhirt_size='.$size.' AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ')
;
}else if($size == ''){
$products->getSelect()
->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
->where('at_int.value=30 AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ' )
;
}
它工作得很好。但这里的代码是重新编写的。如何以这种方式编写上述代码,只有where子句将位于if部分和else部分
请帮帮我。
提前谢谢。喜欢吗
# the same part
$q = $products->getSelect()
->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id');
if($size != ''){
$q->joinLeft(array('cpf'=>'catalog_product_flat_1'),'cpf.entity_id=e.entity_id',array('shhirt_size'))
->where('at_int.value=30 AND cpf.shhirt_size='.$size.' AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ')
;
}else {
$q->where('at_int.value=30 AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ' )
;
}
我使用了Haim Evgi的解决方案,并对其进行了轻微的修正 $subSelect = clone $products->getSelect(); $subSelect->reset() ->from($products->getTable('eav_attribute'), array('attribute_id')) ->where('`attribute_code`="product_attrb_type_name"'); $q = $products->getSelect() ->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id') ->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id') ->where('at_int.value=30') ->where('at_int.attribute_id=?', new Zend_Db_Expr(sprintf('(%s)', $subSelect))); if($size != ''){ $q->joinLeft(array('cpf'=>'catalog_product_flat_1'), 'cpf.entity_id=e.entity_id', array('shhirt_size')) ->where('cpf.shhirt_size=?', $size); }
但是@Aleksandr,当我试图打印\r$q时,我遇到了无法识别的方法“getData”问题;通过使用您的代码。请提出建议。@Srim,如果您想要查看查询,您应该将$q转换为字符串打印。\r字符串$qYes,我在子选择中出错了。请将$products->getTable'eav_attribute'替换为$products->getTable'eav/attribute'并尝试打印$subSelect