使用Zend_Db_Expr选择Zend DB

时间:2014-10-14 08:32:22

标签: php mysql zend-framework

我已经用Zend框架方式转换了以下查询

SELECT 
    DISTINCT 
        eva.errata_number, eva.headline, err.owner,err.rootCause, err.description FROM
            v_allevaluations eva, erratas err 
        WHERE
            eva.errata_number=err.errata_number and stateCombined = 'Affected'  && 
                         (Select releases_id in (SELECT release_id 
                        FROM subscription WHERE user_id = 83) OR folder in (SELECT folder
                        FROM subscribed_ns WHERE user_id = 83))

Zend框架查询:

$select = $db->select()
                        ->distinct()
                        ->from(array('eva'=>'v_allevaluations'),array('errata_number', 'headline'))
                        ->join(array('err'=>'erratas'), 'eva.errata_number= err.errata_number',array('owner','rootCause','description'))
                        ->where('stateCombined=?','Affected')  
                        ->where('select releases_id IN (?)', new Zend_Db_Expr("SELECT release_id FROM subscription WHERE user_id = ".$user_id.") OR folder in (SELECT folder 
                                                                                FROM subscribed_ns WHERE user_id = ".$user_id.")"));

当我打印$ select时打印

 SELECT 
    DISTINCT 
        `eva`.`errata_number`, `eva`.`headline`, `err`.`owner`, `err`.`rootCause`, `err`.`description`
FROM 
    `v_allevaluations` AS `eva` INNER JOIN `erratas` AS `err` ON eva.errata_number= err.errata_number
WHERE 
    (stateCombined='Affected') 
AND 
    (select releases_id IN (SELECT release_id FROM subscription WHERE user_id = 83) OR folder in (SELECT folder FROM subscribed_ns WHERE user_id = 83)))

在user_id = 83之后,它在末尾添加了一个额外的括号。

你可以告诉我有什么问题吗?

1 个答案:

答案 0 :(得分:0)

我将子查询分配给变量并将其替换为查询

 $select = $db->select()
            ->distinct()
            ->from(array('eva'=>'v_allevaluations'),array('errata_number', 'headline'))
            ->join(array('err'=>'erratas'), 'eva.errata_number= err.errata_number',array('owner','rootCause','description'))
            ->where('stateCombined=?','Affected')  
            ->where('select releases_id IN (?)', new Zend_Db_Expr(" ".$get_subscribed_release_ids." ) OR folder in (".$get_subscribed_folders.""));

子查询:

$get_subscribed_release_ids = $db->select()
                                 ->from(array('subscription'), 'release_id')
                                ->where('user_id =?',"$user_id");

$get_subscribed_folders = $db->select()
                             ->from(array('subscribed_ns'), 'folder')
                             ->where('user_id =?',"$user_id");