magento中的concat sql查询

时间:2014-07-08 11:22:50

标签: php mysql database magento

我是magento的新人。我写的代码如下:

   $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());

然后再次使用条件时,通过使用$ products的值,我编写的代码如下:

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部分。

请帮帮我。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

# 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") ' )
    ;
}

答案 1 :(得分:0)

我使用了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);
    }