drt5813 2013-06-30 17:57
浏览 34
已采纳

Symfony2 - 存储库中的自定义查询,用于多对多关系

I have a manyToMany relationship between players and coaches. Each player can have many coaches and each coach can have many players.

I want to create custom query to recuperate all the players that are linked to a particular coach but I really don't know how to do that.

I really need to create a custom query in a repository that will recuperate all the players linked to a particular coach.

Here's the canvas of my repository function in OSC\User\Entity\PlayerRepository

public function findPlayersOfCurrentUser($user) { //The user id is the id linked to the player
        return $this->_em->createQuery('


        ');

    }

All my many-to-many relationships are working and therefore I have a table (named user_player) like the following that is generated:

user_id     player_id
   1            1

Finally, my question would be: What is the SQL query to SELECT all the players in the OSCUserBundle:Player that have $user->getId() as a Coach ?

I know what to do in words(

SELECT all the players from OSCUserBundle:Player WHERE id IN (SELECT all the player_id FROM user_player WHERE user_id = $user->getId()

) but not in via createQuery or createQueryBuilder...

  • 写回答

1条回答 默认 最新

  • doulei2100 2013-07-01 01:53
    关注
    // PlayerRepository
    public function findPlayersOfCurrentUser(User $coach) 
    { 
        return $this->createQueryBuilder("o")
            ->innerJoin("o.coaches", "c", "WITH", "c=:coach")
                ->setParameter("coach", $coach)
            ->getQuery()->getResult() ;
    
    }
    

    Presuming that user and coach are related using aliases coaches and players (plurals, as they should be).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上