为什么这个查询不起作用?

时间:2013-01-07 23:08:37

标签: php zend-framework doctrine

我在Zend Framework中遇到了Doctrine查询问题。构建查询的代码如下所示。

    $qb = $this->createQueryBuilder("m");
    $qb->select("partial m.{_follow,_contacts}");
    //$qb->from("Key\Members\Domain\AbstractMember as m");

    $first = true;
    $types = array();
    for($i = 0; $i < count($networkIds); $i++){
        if($first){
            $first = false;
            $qb->where("m._id = ?".$i);
        } else{
            $qb->orWhere("m._id = ?".$i);
        }
        $qb->setParameter($i, $networkIds[$i], 'binary');
    }

    $this->_logger->log('query:'.$qb->getDQL(), Zend_Log::DEBUG);
    $result = $this->_em->createQuery($qb->getDQL())->getResult();

据我所知,DQL看起来很好:

SELECT partial m.{_follow,_contacts} FROM Key\Members\Domain\Member m WHERE (m._id = ?0) OR (m._id = ?1) OR (m._id = ?2)

$networkIds数组如下所示:

array(3) { [0]=> string(32) "01915617be6f4723b0ed61d6cb30fa5a" [1]=> string(32) "083f375e256244a9accaa176f814333e" [2]=> string(32) "3e43cbafa4174a9fa8197f1c85af812a" }

日志中的例外:

2013-01-07T20:47:28+00:00 ErrorController::errorAction() ERR (3): Controller error occured in for URI "/marketing/reach" in module "portlets", controller "member", action "onecolumn". Exception message was: Invalid parameter format, ? given, but :<name> or ?<num> expected.. Backtrace: #0 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/AST/InputParameter.php(46): Doctrine\ORM\Query\QueryException::invalidParameterFormat('?')
#1 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2116): Doctrine\ORM\Query\AST\InputParameter->__construct('?')
#2 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2253): Doctrine\ORM\Query\Parser->InputParameter()
#3 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2211): Doctrine\ORM\Query\Parser->ArithmeticPrimary()
#4 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2179): Doctrine\ORM\Query\Parser->ArithmeticFactor()
#5 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2153): Doctrine\ORM\Query\Parser->ArithmeticTerm()
#6 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2140): Doctrine\ORM\Query\Parser->SimpleArithmeticExpression()
#7 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2469): Doctrine\ORM\Query\Parser->ArithmeticExpression()
#8 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(2003): Doctrine\ORM\Query\Parser->ComparisonExpression()
#9 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1926): Doctrine\ORM\Query\Parser->SimpleConditionalExpression()
#10 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1885): Doctrine\ORM\Query\Parser->ConditionalPrimary()
#11 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1855): Doctrine\ORM\Query\Parser->ConditionalFactor()
#12 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1831): Doctrine\ORM\Query\Parser->ConditionalTerm()
#13 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1922): Doctrine\ORM\Query\Parser->ConditionalExpression()
#14 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1885): Doctrine\ORM\Query\Parser->ConditionalPrimary()
#15 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1855): Doctrine\ORM\Query\Parser->ConditionalFactor()
#16 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1831): Doctrine\ORM\Query\Parser->ConditionalTerm()
#17 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(1211): Doctrine\ORM\Query\Parser->ConditionalExpression()
#18 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(766): Doctrine\ORM\Query\Parser->WhereClause()
#19 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(735): Doctrine\ORM\Query\Parser->SelectStatement()
#20 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(207): Doctrine\ORM\Query\Parser->QueryLanguage()
#21 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query/Parser.php(278): Doctrine\ORM\Query\Parser->getAST()
#22 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query.php(203): Doctrine\ORM\Query\Parser->parse()
#23 /srv/www/www.cookies.com/current/library/Doctrine/ORM/Query.php(223): Doctrine\ORM\Query->_parse()
#24 /srv/www/www.cookies.com/current/library/Doctrine/ORM/AbstractQuery.php(543): Doctrine\ORM\Query->_doExecute()
#25 /srv/www/www.cookies.com/current/library/Doctrine/ORM/AbstractQuery.php(366): Doctrine\ORM\AbstractQuery->execute(Array, 1)
#26 /srv/www/www.cookies.com/current/library/Key/Members/Repositories/MemberRepository.php(1281): Doctrine\ORM\AbstractQuery->getResult()
#27 /srv/www/www.cookies.com/current/application/modules/marketing/controllers/ReachController.php(53): Key\Members\Repositories\MemberRepository->fetchAllNetworkIds(Array)
#28 /srv/www/www.cookies.com/current/library/Zend/Controller/Action.php(513): Marketing_ReachController->indexAction()
#29 /srv/www/www.cookies.com/current/library/Jarlssen/Rest/Controller.php(59): Zend_Controller_Action->dispatch('indexAction')
#30 /srv/www/www.cookies.com/current/library/Key/Common/Controller/ActionController.php(232): Jarlssen_Rest_Controller->dispatch('indexAction')
#31 /srv/www/www.cookies.com/current/library/Zend/Controller/Dispatcher/Standard.php(295): Key\Common\Controller\ActionController->dispatch('indexAction')
#32 /srv/www/www.cookies.com/current/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#33 /srv/www/www.cookies.com/current/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#34 /srv/www/www.cookies.com/current/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#35 /srv/www/www.cookies.com/current/public/index.php(71): Zend_Application->run()
#36 {main}

我认为问题出现在setParameter()电话的某处,但我无法说出它的错误。我也在没有binary参数的情况下尝试了它,也尝试使用setParameters()。任何想法?

1 个答案:

答案 0 :(得分:1)

问题在于$qb->where("m._id = ?".$i);代替.您必须使用,

所以该行将与

一起使用
  $qb->where("m._id = ?",$i);

  $qb->orWhere("m._id = ?",$i);