开发者

Get Doctrine Relationships in DQL

开发者 https://www.devze.com 2023-01-05 06:28 出处:网络
I have three tables: Project: ... relations: User: local: authorId foreign: id Users: class: User local: projectId

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开发者_如何学Go 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消