douhe4608 2010-06-26 03:47
浏览 31
已采纳

在DQL中获取Doctrine关系

I have three tables:

Project:
  ...
  relations:
    User:
      local: authorId
      foreign: id
    Users:
      class: User
      local: projectId
      foreign: userId
      refClass: UserProjects

User:
  ...
  relations:
    Projects:
      class: Project
      local: userId
      foreign: projectId
      refClass: UserProjects

UserProjects:
  columns:
      id:
        type: integer
        primary: true
        autoincrement: true
      userId: integer
      projectId: integer

What I would like to do is write a DQL statement to return the projects that a user is associated to. I'm trying to emulate the following:

SELECT p.* 
FROM user_projects AS up
LEFT JOIN project AS p ON up.projectid = p.id
LEFT JOIN user AS u ON up.userid = u.id
WHERE u.id = 1

Reading through the Doctrine instructions I came up with the following (u.* is in there because it complained about u not being used in the select statement):

$q = Doctrine_Query::create()
  ->from('Model_User u')
  ->select('u.*, p.*')
  ->leftJoin('u.Projects p');
$result = $q->execute();

What it returns though is a data set containing a single Model_User object with a 'Projects' property filled in with the associated projects. I'd like to just have the projects returned if possible, but I can't seem to figure that out. Is it possible?

  • 写回答

1条回答 默认 最新

  • doubiao7410 2010-07-02 02:31
    关注

    I had my relationships wrong. I had to do the following and I was able to have Doctrine automatically build correct relationships without having to use DQL (So I could go $user->UserProjects or $project->UserProjects)

    Project:
      columns:
        id:
          type: integer
          primary: true
          autoincrement: true
        ...
        authorId: integer
        ...
      relations:
        User:
          local: authorId
          foreign: id
        Users:
          foreignAlias: Projects
          class: User
          refClass: UserProjects
    
    User:
      columns:
        id:
          type: integer
          primary: true
          autoincrement: true
        ...
    
    UserProjects:
      columns:
          id:
            type: integer
            primary: true
            autoincrement: true
          user_id: integer
          project_id: integer
      relations:
        Project:
          foreignAlias: UserProjects
        User:
          foreignAlias: UserProjects
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀