0

以下是我的查询:

$em = \Zend_Registry::get('em');
    $qb_1 = $em->createQueryBuilder();
    $q_1 = $qb_1->select('link_req')
    ->from('\Entities\link_requests','link_req')
    ->where( 'link_req.is_confirmed = 1' )
    ->andWhere('link_req.$link_requestsSenderUser='.$user1_id .'or'.' link_req.$link_requestsSenderUser='.$user2_id)
    ->andWhere('link_req.$link_requestsReceiverUser='.$user1_id .'or'.' link_req.$link_requestsReceiverUser='.$user2_id);
    $result= $q_1->getDql();
    echo $result;

我想要以下查询:

SELECT * FROM [fb_local].[dbo].[link_requests]  
WHERE is_confirmed = 1 AND (request_user_id = 12  or request_user_id=19) AND (accept_user_id = 12 or  accept_user_id = 19)
4

1 回答 1

0

您的 SQL 只是 1 个查询,因此不确定您要嵌套什么。但请尝试以下操作:

$em = \Zend_Registry::get('em');
    $qb_1 = $em->createQueryBuilder();
    $q_1 = $qb_1->select('link_req')
        ->from('\Entities\link_requests','link_req')
        ->where( 'link_req.is_confirmed = 1' )
        ->andWhere('link_req.link_requestsSenderUser=:user_id or link_req.link_requestsSenderUser=:user_id')
        ->andWhere('link_req.link_requestsReceiverUser=:user_id or link_req.link_requestsReceiverUser=:user_id');
    $q_1->setParameter('user_id', $user1_id);
    $query = $q_1->getQuery();
    echo $query->getSql();

请注意我是如何删除$前面的link_requestsReceiverUser. 我还做了一个参数$user1_id,这可以防止SQL 注入

于 2014-01-30T07:03:56.780 回答