简体   繁体   English

如何使用CakePHP3提取关联的belongsToMany实体

[英]How to fetch associated belongsToMany entities with CakePHP3

I have Users and Courses table with belongsToMany relation. 我有用户和课程表与belongsToMany关系。 UserTable has UserTable有

$this->belongsToMany('Courses', [
    'foreignKey' => 'user_id',
    'targetForeignKey' => 'course_id',
    'joinTable' => 'courses_users'
]);

and CoursesTable has 和CoursesTable有

$this->belongsToMany('Users', [
    'foreignKey' => 'course_id',
    'targetForeignKey' => 'user_id',
    'joinTable' => 'courses_users'
]);

Now, I want to fetch courses with user_id. 现在,我想使用user_id获取课程。 In my CoursesController, I tried 在我的CoursesController中,我尝试了

public function myCourses()
{
    $id = $this->Auth->user('id');
    $courses = $this->Courses->find('all',
        ['contain' => ['Users'],
        'condition' => ['Courses.user_id' => $id]
        ]);
    $this->set('courses', $courses);
}

when I debug($courses) with this code, I got '(help)' => 'This is a Query object, to get the results execute or iterate it.' 当我使用这段代码调试($ courses)时,得到了'(help)'=>'这是一个Query对象,用于执行或迭代结果。 message. 信息。 I'm searching information and trying to do it for many hours but I can't make it. 我正在搜索信息,并试图将其花费很多小时,但我做不到。 How can I fetch Courses datas with user_id? 如何使用user_id获取课程数据? Thanks in advance. 提前致谢。

If it's a has-and-belongs-to-many (HABTM) association with a join table of courses_users , you shouldn't even have a user_id field in your Courses table. 如果它是一个多courses_users )关联,并且具有courses_users表, courses_users您的Courses表中甚至不应包含user_id字段。

So now that we've determined you can't do what you were trying ( Courses.user_id ), we can look at what you thought you were trying: 因此,既然我们确定您无法做您尝试的事情( Courses.user_id ),我们可以查看您认为您尝试的事情:

 $courses = $this->Courses->find('all',
     ['contain' => ['Users'],
     //'condition' => ['Courses.user_id' => $id]
 ]);

This says "find all courses and any users that are associated with those courses". 这表示“查找所有课程以及与这些课程相关联的所有用户”。

But what you really WANT (I believe) is: "find all courses that belong to this specific user". 但是,您真正想要的(我相信)是:“找到属于该特定用户的所有课程”。

To do that, you'll want to use an matching() instead. 为此,您将要使用matching()代替。

According to the CakePHP book : 根据CakePHP的书

A fairly common query case with associations is finding records 'matching' specific associated data. 一个常见的关联查询案例是查找与特定关联数据“匹配”的记录。 For example if you have 'Articles belongsToMany Tags' you will probably want to find Articles that have the CakePHP tag. 例如,如果您具有“ Articles EmiratesToMany标签”,则可能要查找具有CakePHP标签的文章。 This is extremely simple to do with the ORM in CakePHP: 使用CakePHP中的ORM极其简单:

$query = $articles->find();
$query->matching('Tags', function ($q) {
    return $q->where(['Tags.name' => 'CakePHP']);
});

So in your case, it would be something like this: 因此,在您的情况下,将是这样的:

$query = $courses->find();
$query->matching('Users', function ($q) use ($id) {
    return $q->where(['Users.id' => $id]);
});

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

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