0

我有三张桌子:

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

我想做的是编写一个 DQL 语句来返回用户关联的项目。我正在尝试模仿以下内容:

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

通读 Doctrine 指令,我想到了以下内容(u.* 在那里,因为它抱怨 u 没有在 select 语句中使用):

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

但它返回的是一个包含单个 Model_User 对象的数据集,该对象具有填充了相关项目的“项目”属性。如果可能的话,我只想让项目返回,但我似乎无法弄清楚。可能吗?

4

1 回答 1

0

我的人际关系错了。我必须执行以下操作,我能够让 Doctrine 自动建立正确的关系,而无需使用 DQL(所以我可以使用 $user->UserProjects 或 $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
于 2010-07-02T02:31:43.520 回答