Symfony2和 Doctrine-Error: PathExpression 无效。必须是 StateFieldPathExpression

我有一个实体,看起来像这样:

/**
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="categories")
* @ORM\Entity()
*/
class Category extends BaseCategory
{


/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
protected $children;


/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $parent;


}

我试着运行这样一个查询:

$qb = $this->em->createQueryBuilder()
->select('c.parent')
->from('Category', 'c');


$result = $qb->getQuery()->getArrayResult();

然而,我得到了以下错误:

[Semantical Error] ... Error: Invalid PathExpression. Must be a StateFieldPathExpression.

如何从表中选择 rent _ id 字段。我已经尝试了很多变化,即使我这样做:

$qb = $this->em->createQueryBuilder()
->select('c')
->from('Category', 'c');

我得到了表 除了中的 rent _ id 的所有字段。这看起来像是教义在妨碍我们。如何查询这个 father _ id 字段?或者更好的是,我怎样才能得到包括 原文在内的表中的 所有字段

112244 次浏览

You can use the currently undocumented IDENTITY function to select the FK IDs in a query:

SELECT IDENTITY(c.parent) ...

Solution using createQueryBuilder:

$query->SELECT('pa.id')
->from('Category', 'ca');
$query->join('ca.parent', 'pa');


$result = $query->getQuery()->getArrayResult();

You are selecting an object that is not joined. Like said in another answer, you have to do something like :

qb->innerJoin("c.parent", "p")

You can change it like this:

 ->select(array('i.id','identity(i.parent) parent','i.nom'))