【问题标题】:Doctrine QueryBulider Syntax ErrorDoctrine QueryBulider 语法错误
【发布时间】:2013-08-02 08:11:39
【问题描述】:

我正在尝试根据从请求中传入的多个值对一个表进行简单查询,但我遗漏了一些对于更有经验的人来说显而易见的东西。

这不起作用:

public function showAction(Request $request)
{
    if ($request->getMethod() == 'GET') {
        $id = $request->get('locationid');
        $kfType = $request->get('type');
        $em = $this->getDoctrine()
                    ->getManager();

        $data = $em->createQueryBuilder()
                    ->select('d')
                    ->from('DashDataBundle:Data',  'd')
                    ->where('d.locationid = :locationid' AND 'd.kfType = :kfType' )
                    ->setParameters(array('locationid'=> $id,'kfType'=> $kfType))
                    ->setMaxResults(100)
                    ->getQuery()
                    ->getResult();
    }

错误是:
警告:get_class() 期望参数 1 是对象,布尔值在 /Applications/MAMP/htdocs/path/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Expr/Base.php 第 89 行中给出

但是,这仅适用于一个参数:

public function showAction(Request $request)
{
    if ($request->getMethod() == 'GET') {
        $id = $request->get('locationid');
        $kfType = $request->get('type');
        $em = $this->getDoctrine()
                    ->getManager();

        $data = $em->createQueryBuilder()
                    ->select('d')
                    ->from('DashDataBundle:Data',  'd')
                    ->where('d.locationid = :locationid')
                    ->setParameter('locationid', $id)
                    ->setMaxResults(100)
                    ->getQuery()
                    ->getResult();
    }

我不明白什么?

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    你正在用 AND 组合两个字符串,这就是为什么它抱怨得到一个布尔值。删除多余的引号,以便 AND 成为 where 函数中单个字符串参数的一部分。

    【讨论】:

    • 必须是->where('d.locationid = :locationid AND d.kfType = :kfType' )
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2015-10-31
    • 2016-10-19
    • 2020-03-21
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多