如何使用CreatequeryBuilder在Doctrine2中嵌套两个和查询



以下是我的查询:

$em = Zend_Registry::get('em');
    $qb_1 = $em->createQueryBuilder();
    $q_1 = $qb_1->select('link_req')
    ->from('Entitieslink_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)

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

$em = Zend_Registry::get('em');
    $qb_1 = $em->createQueryBuilder();
    $q_1 = $qb_1->select('link_req')
        ->from('Entitieslink_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注入。

相关内容

  • 没有找到相关文章

最新更新