symfony错误:预期=,<,< =,<>,>,> =,!=,得到'。'

时间:2015-07-07 15:49:33

标签: php symfony doctrine-orm

我有一个查询,查找从控制器中的变量获得的结果。

server {
  listen         80;
  server_name example.com *.example.com;

  charset utf-8;

  gzip on;
  gzip_disable "msie6";

  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  rewrite "^(.*/)index\.html$" $1 permanent;

  location / {
    root /srv/www/example.com/;
    index index.html;
  }

  error_page 404 /404/;
}

但是当我为查询运行操作时,我发生了这个错误:

$category = $request->request->get('category');

$catContent = $em->getRepository('PublicartelAppBundle:Content')->findByCategory($category);

public function findByCategory($category)
    {
        $em = $this->getEntityManager();

        $dql = 'SELECT c FROM Publicartel\AppBundle\Entity\Content c';

        if (!is_null($category)) {
            $dql .= " WHERE c.categories.name = category";
        }

        $query = $this->getEntityManager()
            ->createQuery($dql)
            ->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);

        if (!is_null($category)) {
            $query->setParameter('category', $category);
        }

        return $query->getResult();
    }

SELECT c FROM Publicartel \ AppBundle \ Entity \ Content c WHERE c.categories.name = category

$ categories是实体内容的属性。

[Syntax Error] line 0, col 71: Error: Expected =, <, <=, <>, >, >=, !=, got '.'

奇怪的是,当我得到变量时,错误不会出现。

class Content
{
private $categories;

    public function __construct()
    {
        $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
    }

public function addCategories(\Publicartel\AppBundle\Entity\Category $categories)
    {
        $this->categories[] = $categories;

        return $this;
    }

    public function removeCategories(\Publicartel\AppBundle\Entity\Category $categories)
    {
        $this->categories->removeElement($categories);
    }

    public function setCategories(\Publicartel\AppBundle\Entity\Category $categories = null)
    {
        $this->categories = $categories;

        return $this;
    }
    public function getCategories()
    {
        return $this->categories;
    }
}

1 个答案:

答案 0 :(得分:2)

您不能以这种方式引用子对象:

SELECT c FROM Publicartel\AppBundle\Entity\Content c WHERE c.categories.name = :category

在此之前您需要JOIN并使用该已加入实体的别名。像这样:

SELECT c, cat FROM Publicartel\AppBundle\Entity\Content c JOIN c.categories cat WHERE cat.name = :category

现在,要么使用LEFT JOIN,要么只使用JOIN,它应该有用。