我有三张桌子:

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

仔细阅读我想到的学说说明(u。

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

它返回的是一个包含单个model_user对象的数据集,其中包含带有关联项目的“项目”属性。如果可能的话,我想返回项目,但我似乎无法弄清楚。是否可以?

有帮助吗?

解决方案

我的关系错了。我必须执行以下操作,并且我能够自动建立正确的关系而无需使用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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top