简体   繁体   English

原则创建具有绑定参数的子查询

[英]Doctrine create subquery with bound params

I am trying to construct a query that will basically pull all entries whose id's have 2 particular entries in another table so I am trying the below: 我正在尝试构建一个查询,该查询将基本上拉出其ID在另一个表中具有2个特定条目的所有条目,因此我尝试了以下操作:

    $query      = $this->createQuery('e');

    $subquery1 = $query->createSubquery('sea')
                       ->select('sea.entry_id')
                       ->from('Table sea')
                       ->addWhere('sea.form_element_id = ?', $element)
                       ->addWhere('sea.answer = ?', $answer)
                       ->getDQL();

    $subquery2 = $query->createSubquery('sea2')
                       ->select('sea2.entry_id')
                       ->from('Table sea2')
                       ->addwhere('sea2.form_element_id = ?', $element2)
                       ->addWhere('sea2.answer = ?', $answer2)
                       ->getDQL();

    $query->addWhere('e.id IN ?', $subquery1)
          ->addWhere('e.id IN ?', $subquery2);

    return $query->execute();

However this is gives me an error on bound params. 但是,这给了我绑定参数的错误。

What is the correct ways of constructing such subqueries? 构造此类子查询的正确方法是什么?

NOTE that if I dont bind the params in the subqueries it works fine. 请注意,如果我不在子查询中绑定参数,则可以正常工作。

$nestedQuery = " id IN (SELECT sea.entry_id from table sea where sea.form_element_id = ? and sea.answer = ?) "
                . " and id IN (SELECT sea2.entry_id from table sea2 where sea2.form_element_id = ? and sea2.answer = ?)";


    return $this->findBySql($nestedQuery, array($param1, $param2, $param3, $param4));

That obviously returns a doctrine collection but you can do getFirst or loop through the returned objects or even use the Hydrator to get an array! 显然,这返回了一个学说集合,但是您可以执行getFirst或遍历返回的对象,甚至使用Hydrator来获取数组!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM