无效的参数号:绑定变量的数量与 Doctrine 中的标记数量不匹配

Invalid parameter number: number of bound variables does not match number of tokens in Doctrine(无效的参数号:绑定变量的数量与 Doctrine 中的标记数量不匹配)
本文介绍了无效的参数号:绑定变量的数量与 Doctrine 中的标记数量不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Doctrine 2 我想获取一些用户是另一个用户的联系人.user 表包含这些用户之间的映射.函数中的查询会返回如下错误:

Using Doctrine 2 I want to get some users that are contacts of another user. The table user contains the mapping between those users. The query in the function will return the following error:

参数号无效:绑定变量的数量与标记的数量不匹配.

Invalid parameter number: number of bound variables does not match number of tokens.

但据我所知,$str 设置为b",$ownerId 设置为2",两者均由 setParameters函数.

However to my best understanding $stris set to "b" and $ownerId is set to "2" and both are assigned by the setParameters function.

 protected function getContactBySubstring($str, $ownerId) {
        echo $str;
        echo $ownerId;
        $em = $this->getDoctrine()->getEntityManager();
        $qb = $em->createQueryBuilder();
        $qb->add('select', 'u')
                ->add('from', 'PastonVerBundleEntityUser u, PastonVerBundleEntityContact c')
                ->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
                ->add('orderBy', 'u.firstname ASC, u.lastname ASC')
                ->setParameters(array (1=> $ownerId, 2=> '%'.$str.'%'));

        echo $qb->getDql();
        $query = $qb->getQuery();
        $users = $query->getResult();
        foreach($users as $user)
            echo $user->getUsername();
        exit;
        //return $contacts;
    }

推荐答案

不要用引号将查询文本中的任何参数括起来!

Don't surround any of the parameters in your query text with quotes!

->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")

应该是

->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE ?2")

这篇关于无效的参数号:绑定变量的数量与 Doctrine 中的标记数量不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)