Symfony2 Doctrine错误:无效的PathExpression。必须是StateFieldPathExpression

时间:2014-05-17 16:37:31

标签: php symfony doctrine-orm query-builder

我正在寻找可以解决我的问题的任何帮助,但毕竟我仍然得到同样的错误。

我正在尝试从我的数据库中获取一些选定类别的新闻。 新闻和类别之间的关系比Doctrine多对多。

<?php
//News
namespace Core\DatabaseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class SfNews
{
    private $date;
    private $header;
    private $content;
    private $contentPlain;
    private $id;
    private $sfUsers;
    private $sfUploadedFiles;
    private $sfCategories;
    public function __construct()
    {
        $this->sfUploadedFiles = new \Doctrine\Common\Collections\ArrayCollection();
        $this->sfCategories    = new \Doctrine\Common\Collections\ArrayCollection();
    }
    // Some code removed
    public function setSfUsers(\Core\DatabaseBundle\Entity\Users $sfUsers = null)
    {
        $this->sfUsers = $sfUsers;
        return $this;
    }
    public function getSfUsers()
    {
        return $this->sfUsers;
    }
    public function addSfUploadedFile(\Core\DatabaseBundle\Entity\SfUploadedFiles $sfUploadedFiles)
    {
        $this->sfUploadedFiles[] = $sfUploadedFiles;
        return $this;
    }
    public function removeSfUploadedFile(\Core\DatabaseBundle\Entity\SfUploadedFiles $sfUploadedFiles)
    {
        $this->sfUploadedFiles->removeElement($sfUploadedFiles);
    }
    public function getSfUploadedFiles()
    {
        return $this->sfUploadedFiles;
    }
    public function addSfCategorie(\Core\DatabaseBundle\Entity\SfCategories $sfCategories)
    {
        $this->sfCategories[] = $sfCategories;
        return $this;
    }
    public function removeSfCategorie(\Core\DatabaseBundle\Entity\SfCategories $sfCategories)
    {
        $this->sfCategories->removeElement($sfCategories);
    }
    public function getSfCategories()
    {
        return $this->sfCategories;
    }
}

//Categories
namespace Core\DatabaseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class SfCategories
{
    private $name;
    private $id;
    //code removed
    public function getId()
    {
        return $this->id;
    }
    private $sfNews;
    public function __construct()
    {
        $this->sfNews = new \Doctrine\Common\Collections\ArrayCollection();
    }
    public function addSfNews(\Core\DatabaseBundle\Entity\SfNews $sfNews)
    {
        $this->sfNews[] = $sfNews;
        return $this;
    }
    public function removeSfNews(\Core\DatabaseBundle\Entity\SfNews $sfNews)
    {
        $this->sfNews->removeElement($sfNews);
    }
    public function getSfNews()
    {
        return $this->sfNews;
    }
    //code removed
}

我尝试使用的代码是:

$Sfcategory = $this->em->getRepository("CoreDatabaseBundle:SfCategories")->findOneBy(array("url"=>$category));
    $qb = $this->em->createQueryBuilder('p');
    $qb
       ->select('p')
       ->from("CoreDatabaseBundle:SfNews","p")
       ->where($qb->expr()->like('p.sfCategories', ':id'))
            ->setParameter('id', $Sfcategory->getId())
       ->setMaxResults($count)
       ->orderBy('p.date', 'DESC');
    $r = $qb->getQuery();

    return $r->getResult();

通过XML进行ORM

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Core\DatabaseBundle\Entity\SfNews" table="sf_news">
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="date" type="datetime" column="date" nullable="true"/>
    <field name="header" type="string" column="header" length="255" nullable="false"/>
    <field name="content" type="text" column="content" nullable="false"/>
    <field name="contentPlain" type="text" column="content_plain" nullable="false"/>
    <one-to-one field="sfUsers" target-entity="Users">
      <join-columns>
        <join-column name="sf_users_id" referenced-column-name="id"/>
      </join-columns>
    </one-to-one>
    <many-to-many field="sfUploadedFiles" target-entity="SfUploadedFiles" inversed-by="sfNews">
      <join-table name="sf_news_has_sf_uploaded_files">
        <join-columns>
          <join-column name="sf_news_id" referenced-column-name="id"/>
        </join-columns>
        <inverse-join-columns>
          <join-column name="sf_uploaded_files_id" referenced-column-name="id"/>
        </inverse-join-columns>
      </join-table>
    </many-to-many>
    <many-to-many field="sfCategories" target-entity="SfCategories" inversed-by="sfNews">
      <join-table name="sf_news_has_sf_categories">
        <join-columns>
          <join-column name="sf_news_id" referenced-column-name="id"/>
        </join-columns>
        <inverse-join-columns>
          <join-column name="sf_categories_id" referenced-column-name="id"/>
        </inverse-join-columns>
      </join-table>
    </many-to-many>
  </entity>
</doctrine-mapping>

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Core\DatabaseBundle\Entity\SfCategories" table="sf_categories">
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="name" type="string" column="name" length="255" nullable="false"/>
    <field name="url" type="string" column="url" length="255" nullable="false"/>
    <field name="visible" type="boolean" column="visible" nullable="true"/>
    <many-to-many field="sfNews" target-entity="SfNews" mapped-by="sfCategories"/>
  </entity>
</doctrine-mapping>

我得到的错误是:

[Semantical Error] line 0, col 50 near 'sfCategories': Error: Invalid PathExpression. Must be a StateFieldPathExpression. 

我期待以任何方式解决问题。 感谢

0 个答案:

没有答案