【问题标题】:query for not null OneToMany relationship in symfony / doctrine在 symfony / 学说中查询不为空的 OneToMany 关系
【发布时间】:2017-11-03 15:31:51
【问题描述】:

我正在使用 Symfony 3.2。我有一个名为Site 的实体,它包含与另一个名为Indication 的实体的OneToMany 关系。

class Site
{
    /**
     * @ORM\OneToMany(targetEntity="IhkBundle\Entity\Indication", mappedBy="site")
     */
    private $indications;
}

class Indication
{
    /**
     * @ORM\ManyToOne(targetEntity="IhkBundle\Entity\Site", inversedBy="indications")
     * @ORM\JoinColumn(name="site_id", referencedColumnName="id")
     */
    private $site;
}

我想查询所有sites 可用的Indications。我只得到一个我不知道如何处理的 ArrayCollection。

$sites = $repository->findAll();
foreach ($sites as $site) {
   $site->getIndications();
}

我也尝试使用 this answer 中的 queryBuilder。

$query = $repository->createQueryBuilder('s');
$result = $query->where('s.indications IS NOT NULL')
    ->getQuery()
    ->getResult();

抛出以下错误:

[Semantical Error] line 0, col 46 near 'indications IS': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

【问题讨论】:

    标签: php sql symfony doctrine-orm


    【解决方案1】:

    似乎这可以通过SQL通过简单的连接来解决,这样的事情应该可以工作:

    $query = $repository->createQueryBuilder('s');
    $result = $query->select('p')
        ->join('p.indications', 'i')
        ->getQuery()
        ->getResult();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      相关资源
      最近更新 更多