简体   繁体   English

获取DQL中的教义关系

[英]Get Doctrine Relationships in DQL

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. 我想做的是编写一条DQL语句以返回与用户关联的项目。 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): 通读《主义》说明,我想到了以下内容(其中存在u。*,因为它抱怨在select语句中未使用u):

$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. 它返回的是一个包含单个Model_User对象的数据集,该对象的“ 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) 我必须执行以下操作,并且能够使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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM