php - 参数太少 : the query defines 1 parameters but you only bound 0

标签 php symfony doctrine-orm

Symfony 版本 3.2.8

根据 Doctrine Documentation,我不确定是什么导致了这个错误setParameter 函数是否正确使用?

损坏的代码:

public function getNewShipChoices($uid, $fid) {
        /*Identify ships all ready added in fleet and do not allow them to be added again*/
        $q2 = $this->createQueryBuilder('c')
                    ->select('DISTINCT (c2.shipId)')
                    ->join('AppBundle:ShipsFleet', 'c2')
                    ->where('c.userid = :uid')->setParameter('uid', $uid)
                    ->andWhere('c2.fleetId = :fid')->setParameter('fid', $fid);

        $query = $this->createQueryBuilder('c3');
        $query->where($query->expr()->notIn('c3.shipId', $q2->getDQL()))->andWhere('c3.userid = :uid')->setParameter('uid', $uid);

        return $query->getQuery()->getResult();
    }

我尝试的另一件事是对 setParameter 值进行硬编码,这会带来相同的错误消息

 ->where('c.userid = :uid')->setParameter('uid', 1)
                            ->andWhere('c2.fleetId = :fid')->setParameter('fid', 1);

工作代码: 将 setParameter 替换为硬编码值而不是传入 1 和 1 的 2 个整数值可以正常工作。

 public function getNewShipChoices($uid, $fid) {
        $q2 = $this->createQueryBuilder('c')
                    ->select('DISTINCT (c2.shipId)')
                    ->join('AppBundle:ShipsFleet', 'c2')
                    ->where('c.userid = 1')
                    ->andWhere('c2.fleetId = 1');

        $query = $this->createQueryBuilder('c3');
        $query->where($query->expr()->notIn('c3.shipId', $q2->getDQL()))->andWhere('c3.userid = 1');

        return $query->getQuery()->getResult();
    }

我是否遗漏了一些非常明显的东西?

最佳答案

我认为实际问题是由 doctrine 的 ->getDQL 命令没有传递参数值引起的。它传递预期的参数,但仅在执行时将它们混合。

在你的例子中,你在已经设置了 fid 参数之后将一些 DQL 传递给了 select 但没有在执行的查询中重新设置该参数,Doctrine 不知道这一点参数 so 会按预期抛出错误。

修复方法是:

public function getNewShipChoices($uid, $fid) {
    /*Identify ships all ready added in fleet and do not allow them to be added again*/
    $q2 = $this->createQueryBuilder('c')
                ->select('DISTINCT (c2.shipId)')
                ->join('AppBundle:ShipsFleet', 'c2')
                ->where('c.userid = :uid')
                ->andWhere('c2.fleetId = :fid');

    $query = $this->createQueryBuilder('c3');
    $query->where($query->expr()->notIn('c3.shipId', $q2->getDQL()))
        ->andWhere('c3.userid = :uid')
        ->setParameter('uid', $uid)
        ->setParameter('fid', $fid);;

    return $query->getQuery()->getResult();
}

请注意,$q2 没有设置参数,因为在传递 DQL 时这些参数将被丢弃。

关于php - 参数太少 : the query defines 1 parameters but you only bound 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44412692/

相关文章:

php - SonataAdminBundle configureShowFields 与嵌套实体

php - 对 Doctrine 中的实体使用自定义查询 - Symfony Form

mysql - Symfony2、Doctrine2、MySql 和 auto_increment 列问题

php - Symfony 2 - 防火墙和访问控制问题

php - 我可以从 doctrine2 中的 php 访问鉴别器字段吗?

php - Symfony/Doctrine - 使用外部数据源使数据库保持最新

php - 将参数从一个 PHP 页面传递到另一个页面 - 它总是获得最大的 (MySQL)

php - 如何在我的示例中使用 urlencode()?

php - WordPress:- get_the_date() 函数返回错误的日期

php - Symfony 4 Form - Ajax 选择人口