[英]Cakephp 3.6 contain
I have on little problem with my Cakephp's project. 我的Cakephp项目几乎没有问题。 I have this get function in StudentController for view action.
我在StudentController中有此get函数,用于查看操作。
$student = $this->Students->get($id, [
'contain' => ['Courses', 'LessonPresences', 'StudentNotes', 'StudentPayments']
]);
StudentNotes are added by Users. 学生笔记由用户添加。 With the contain I get the StudentNotes with the ID of the user who add this note.
有了contains,我得到了带有添加该笔记的用户ID的StudentNotes。 How I can get the name of this user ?
我如何获得该用户的名字?
Student {#711 ▼
+"id": 1
+"name": "John"
+"lastname": "Doe"
+"email": "johndoe@gmail.com"
+"phone": "111222333"
+"address": "Brown Street"
+"add_date": FrozenTime @1531866000 {#347 ▶}
+"sign_date": FrozenDate @1531699200 {#350 ▶}
+"theory_count": 65
+"course_end": null
+"course_id": 1
+"student_notes": array:1 [▼
0 => StudentNote {#607 ▼
+"id": 1
+"s_content": "Test student's note"
+"add_date": FrozenTime @1532677715 {#606 ▶}
+"student_id": 1
+"add_user_id": 1
I have "add_user_id" which is "1". 我的“ add_user_id”为“ 1”。 How can I get the name of this user?
如何获得此用户的名称? There is an belongsTo association in StudentNotesTable
有一个在StudentNotesTable一个属于关联关联
$this->belongsTo('Users', [
'foreignKey' => 'add_user_id',
'joinType' => 'INNER'
]);
simply add 'Users'
to the contain array. 只需将
'Users'
添加到包含数组。 You can do it in many ways, the following are all equivalent 您可以通过多种方式进行操作,以下各项均等效
Using dotted notation 使用虚线符号
'contain' => [
'Courses',
'LessonPresences',
'StudentNotes',
'StudentNotes.Users',
'StudentPayments',
]
Using array 使用数组
'contain' => [
'Courses',
'LessonPresences',
'StudentNotes' => ['contain' => ['Users']],
'StudentPayments',
]
Using callable 使用可调用
'contain' => [
'Courses',
'LessonPresences',
'StudentNotes' => function ($q) {
return $q->contain(['Users']);
},
'StudentPayments',
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.