【发布时间】:2017-07-15 23:20:23
【问题描述】:
我有带有 belongsToMany 关系的用户和课程表。 UserTable 有
$this->belongsToMany('Courses', [
'foreignKey' => 'user_id',
'targetForeignKey' => 'course_id',
'joinTable' => 'courses_users'
]);
CoursesTable 有
$this->belongsToMany('Users', [
'foreignKey' => 'course_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'courses_users'
]);
现在,我想使用 user_id 获取课程。在我的 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);
}
当我使用这段代码调试($courses) 时,我得到了 '(help)' => '这是一个 Query 对象,让结果执行或迭代它。'信息。我正在搜索信息并尝试了很多小时,但我做不到。如何使用 user_id 获取课程数据?提前致谢。
【问题讨论】:
标签: php cakephp cakephp-3.0