Doctrine2,setParameter没有按预期工作

时间:2017-10-09 05:55:22

标签: php mysql doctrine-orm

我有简单的查询构建器,看起来像tihs:

    $qb2 = $this->createQueryBuilder('d')
        ->select('a.name, c.externalId, c.id, c.name, d.name')
        ->join('d.urls', 'u')
        ->distinct('c.externalId')

        ->join('u.keywords', 'k')
        ->join('k.adGroup', 'ag')
        ->join('ag.campaign', 'c')
        ->join('c.account', 'a')
        ->where('a.isActive = 1 and c.status = :status and d.name LIKE :domain');

    $query2 = $qb2->getQuery();
    $query2->setParameter('domain', "'%somedomain%'");
    $query2->setParameter('status', 'ENABLED');

并生成以下查询

SELECT
    a0_.name AS name_0,
    c1_.external_id AS external_id_1,
    c1_.id AS id_2,
    c1_.name AS name_3,
    u2_.name AS name_4
FROM
    url_domain u2_
INNER JOIN url_to_domain u4_ ON
    u2_.id = u4_.url_domain_id
INNER JOIN url u3_ ON
    u3_.id = u4_.url_id
INNER JOIN keyword k5_ ON
    u3_.id = k5_.url_id
INNER JOIN ad_group a6_ ON
    k5_.ad_group_id = a6_.id
INNER JOIN campaign c1_ ON
    a6_.campaign_id = c1_.id
INNER JOIN account a0_ ON
    c1_.account_id = a0_.id
WHERE
    a0_.is_active = 1 AND c1_.status = ? AND u2_.name LIKE ?

当我像这样使用它时,我得到0结果,但当我把事情放在双引号的地方,如"a.isActive = 1 and c.status = 'ENABLED' and d.name LIKE '%{$somedomain}%'"我得到了理想的结果我做错了什么?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

您将参数绑定在错误的位置。您应该在调用getQuery()之前绑定他们。这样的事情应该有效:

$qb2 = $this->createQueryBuilder('d')
    ->select('a.name, c.externalId, c.id, c.name, d.name')
    ->join('d.urls', 'u')
    ->distinct('c.externalId')
    ->join('u.keywords', 'k')
    ->join('k.adGroup', 'ag')
    ->join('ag.campaign', 'c')
    ->join('c.account', 'a')
    ->where('a.isActive = 1 and c.status = :status and d.name LIKE :domain');
    ->setParameter('domain', '%'.$some_domain.'%');
    ->setParameter('status', 'ENABLED');

$query2 = $qb2->getQuery();