Symfony2存储库查询无效

时间:2012-05-10 18:02:51

标签: symfony doctrine doctrine-orm dql

我正在使用Symfony2和DQL开发一个应用程序,用于在存储库中构建一些查询。我在控制器中有下一个代码:

$emGalPak = $this->getDoctrine()->getEntityManager();
  $OsatugabeKop = $emGalPak->getRepository('AnotatzaileaAnotatzaileaBundle:GalderaPaketea')
                             ->getOsatugabeKop(); 

这是我在存储库中构建的与上述实体相对应的查询:

<?php

namespace Anotatzailea\AnotatzaileaBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * GalderaPaketeaRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class GalderaPaketeaRepository extends EntityRepository
{
    public function getOsatugabeKop()
    {
        $qb = $this->createQueryBuilder('c')
                   ->select('c')
                   ->where('c.Osatua = 0')
        $Emaitza = $qb->getQuery()->getResult();

                return sizeof($Emaitza);

    }

}

运行代码时会显示下一个错误:

Parse error: syntax error, unexpected T_VARIABLE in /var/www/Symfony/src/Anotatzailea/AnotatzaileaBundle/Repository/GalderaPaketeaRepository.php on line 20

关于如何解决此错误的任何想法?

2 个答案:

答案 0 :(得分:2)

这与您的查询无法正常工作无关。

当你看到&#34;解析错误&#34;这意味着你的PHP代码本身格式不正确,PHP引擎甚至无法解析它,更不用说运行它了。

在这种特殊情况下,您在创建查询构建器的表达式末尾缺少分号。

public function getOsatugabeKop()
{
    $qb = $this->createQueryBuilder('c')
               ->select('c')
               ->where('c.Osatua = 0'); // <--- right there
    $Emaitza = $qb->getQuery()->getResult();

    return sizeof($Emaitza);
}

当你得到unexpected T_VARIABLE错误时,几乎总是因为你省略了分号并且解析器在它认为应该之前遇到了一个变量。如果你拿出空白,就会更容易看出错误。

// Bad Code, two lines
$now = time()
$one = 1;

// Bad Code, one line
$now = time()$one = 1;
// ----------^  Pretty obvious now that a semicolon is missing
// And that a variable was encountered unexpectedly

干杯

答案 1 :(得分:0)

where行之后缺少分号。

相关问题