学说-父子查询结果关系

时间:2019-04-02 12:28:48

标签: php doctrine-orm doctrine entity symfony-3.4

我正在尝试使用单个实体提取数据树。父母是孩子关系。

但是,我的数据库不是空的,但是结果返回的是空数组结果。

有什么建议吗?

这是我的实体:(还包括getter和setters)

class MyClass  
{

public function __construct()
{
    $this->children = new ArrayCollection();
}

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="MyClass", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
 * @Groups({"data"})
 */
private $parentAccount;

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

还有我的查询生成器:

$query = $this->getMyClassRepository()
        ->createQueryBuilder('q')
        ->leftJoin('q.parentAccount', 'q')
        ->where('q.children = :children')
        ->getQuery();

    return $query;

1 个答案:

答案 0 :(得分:0)

我认为您必须为孩子设置一个值:

   $query = $this->getMyClassRepository()
            ->createQueryBuilder('q')
            ->leftJoin('q.parentAccount', 'q')
            ->where('q.children = :children')
            ->setParameter('children', $children)

            ->getQuery();

        return $query;
相关问题