doujiena0025
2017-06-02 09:19
浏览 77
已采纳

在symfony / doctrine中查询not null OneToMany关系

I am using Symfony 3.2. I have an entity called Site which contains a OneToMany relationship to another entity called Indication.

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;
}

I want to query for all sites where Indications are available. I only get an ArrayCollection which I don't know what to do with.

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

I also tried to use the queryBuilder like in this answer.

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

which throws the following error:

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

图片转代码服务由CSDN问答提供 功能建议

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

 类Site 
 {
 / ** 
 * @ORM \ OneToMany(targetEntity =”IhkBundle \ Entity \ Indication“,mappedBy =”site“)
 * / 
 private $ indication  ; 
} 
 
class指示
 {
 / ** 
 * @ORM \ ManyToOne(targetEntity =“IhkBundle \ Entity \ Site”,inversedBy =“indication”)
 * @ORM \ JoinColumn(name  =“site_id”,referencedColumnName =“id”)
 * / 
 private $ site; 
} 
   
 
 

我想查询所有 站点,其中 Indications 可用。 我只得到一个我不知道该怎么做的ArrayCollection。

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

我也尝试使用queryBuilder 比如这个答案

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

会引发以下错误:

   [Semantical Error]第0行,col 46附近'指示IS':错误:无效的PathExpression。 期望StateFieldPathExpression或SingleValuedAssociationField。
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqiongjiong4740 2017-06-02 09:28
    已采纳

    Seems like this can be solved by SQL by doing a simple join, something like this should work:

    $query = $repository->createQueryBuilder('s');
    $result = $query->select('p')
        ->join('p.indications', 'i')
        ->getQuery()
        ->getResult();
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题