0

$我正在尝试使用学说 2 查询生成器创建以下场景

SELECT 
    p . *
FROM
    _tree p
    LEFT JOIN
    _tree c ON p.id = c.parent_id
       AND (c.lft >= p.lft AND c.rgt <= p.rgt)
WHERE
    p.id = 3 

我已经设置了由 Doctrine2 生成的以下关系

    类树 {

    /**
     * @var \树
     *
     * @ORM\ManyToOne(targetEntity="Tree")
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     * })
     */
    私人$父母;

      // 其他代码
    }

这是我的回购课程

_em->createQueryBuilder();
        $qb->选择('p')
           ->从('实体\树','p')
           ->leftJoin('p.Entity\Tree','c', 'ON','p.id = c.parent_id');

        返回 $qb->getQuery()->getResult();
    }
}

但我无法完成它。它引发以下错误

[2013 年 10 月 1 日星期二 22:30:11] [错误] [客户端 127.0.0.1] PHP 致命错误:未捕获的异常 'Doctrine\ORM\Query\QueryException' 带有消息 'SELECT p FROM Entity\Tree p LEFT JOIN p.Entity \Tree c ON p.id = c.parent_id' 在 /var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php:39\n堆栈跟踪:\n#0 /var/www/ pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(429): Doctrine\ORM\Query\QueryException::dqlError('SELECT p FROM E...')\n#1 /var/www /pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(925): Doctrine\ORM\Query\Parser->semanticalError('Class Entity\Ed...')\n#2 /var/ www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1561): Doctrine\ORM\Query\Parser->JoinAssociationPathExpression()\n#3 /var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1506): Doctrine\ORM\Query\Parser->JoinAssociationDeclaration()\n#4 / var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/Parser.php(1435): Doctrine\ORM\Query\Parser->Join()\n#5 /var/www/pcb_frame_work/System/第 49 行 /var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php 中的库,引用者:Join()\n#5 /var/www/pcb_frame_work/System/Librari in /var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php 在第 49 行,引用:Join()\n#5 /var/www/pcb_frame_work/System/Librari in /var/www/pcb_frame_work/System/Libraries/Vendors/Doctrine/ORM/Query/QueryException.php 在第 49 行,引用:

4

1 回答 1

1

我不知道我是否完全理解您,但我认为您必须将您的 ManyToOne 关系更改为:

/**
 * @var \Tree
 *
 * @ORM\ManyToOne(targetEntity="Tree", inversedBy="children")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
 * })
 */
private $parent;

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

这样你就可以访问一个类的子类,$Tree->children它的父类是$Tree->parent.

更多关于自引用关联的信息可以在这里找到:http: //docs.doctrine-project.org/en/latest/reference/association-mapping.html#one-to-many-self-reference

于 2013-10-02T12:55:42.843 回答