views:

54

answers:

1

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?

A: 

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
dragonmantank