[英]Display a field from a 3rd table
I am trying to display a field from a 3rd table in a relationship, and after checking posts here and the docs, I am still stuck.I have 3 models all related in some way. 我试图显示关系中第三个表中的字段,并且在检查此处的帖子和文档后,我仍然卡住了。我有3个模型都以某种方式相关。 I have found similar posts here but I am still not getting it to work.
我在这里找到了类似的帖子,但仍然无法正常工作。 I am learning so sorry if I have missed this somewhere in the docs but I have read a fair bit and have tried a lot.
如果我在文档中的某个地方错过了此功能,我正在学习,很抱歉,但是我已经读了很多,并做了很多尝试。 I am guessing too much now on this so I need help.
我现在对此猜测太多,因此我需要帮助。
1)Tutorsession - belongsto teachers,
2) Teacher -has 1 user,hasmany tutorsessions
3) User- has 1 teacher, //////I want to display a field from this table given I display tutorsessions
In controller
$this->set('tutor',
$this->Tutorsession->find('first',
array(
'conditions' => array('Teacher.user_id' => $id),
'contain' => 'User.username'
)
)
); //////////no error but no results
from view echo '<td>'. $item['User']['username'].'</td>'; ///////error user undefined
The tutorsession automatically gets rows from teacher table but not user table witht he model setup on a findall. 如果在findall上进行模型设置,则tutorial会自动从教师表而不是用户表中获取行。
I want to display a username from the user table. 我想显示用户表中的用户名。 I display the tutorsession table , I then can display the teacher table with the model association but I cant go from the teacher table to the user table to get a user name from the user id as the common field.
我显示了tutorsession表,然后可以显示具有模型关联的教师表,但是我不能从教师表转到用户表以从用户ID获取用户名作为公用字段。 I have checked the docs below and I am not sure why my code isnt ble to display a username from users table.
我检查了下面的文档,但不确定为什么我的代码无法显示users表中的用户名。
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html http://book.cakephp.org/2.0/en/models/retrieving-your-data.html http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
update: here are the models
class User extends AppModel {
public $hasOne = array(
'Teacher' => array(
'className' => 'Teacher',
'dependent' => true
)
class Tutorsession extends AppModel
{
public $name='Tutorsession';
public $belongsTo = array(
'Teacher' => array(
'className' => 'Teacher',
'foreignKey' => 'teacher_id'
)
class Teacher extends AppModel
{
public $name='Teacher';
public $hasMany = array('Tutorsession');
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
Try this: 尝试这个:
$this->Tutorsession->recursive = 2;
$this->Tutorsession->Teacher->contain('User');
$this->set('tutor',
$this->Tutorsession->find('first',
array(
'conditions' => array('Teacher.user_id' => $id)
)
)
);
$tutor=$this->Teacher->find('first',
array(
'conditions' => array('Teacher.user_id' => $id)
)
);
debug($tutor); exit();
Your result similar: 您的结果类似:
'Teacher' => array(
'id' => '1',
'name' => 'abc',
'created' => '1397040385',
'updated' => '1397725860'
),
'User' => array(
'id' => '4',
'name' => 'administrators',
'created' => '1397032953',
'modified' => '1397032953'
),
'Tutorsession' => array(
0=>array(
'id' => '3',
'name'=>'abc',
'created' => '1400729137'
),
1=>array(
'id' => '4',
'name'=>'abc',
'created' => '1400729137'
)
)
Ensure your models are correct 确保您的模型正确
Add containable behavior in your Model 在模型中添加可包含的行为
public $actsAs = array('Containable');
Execute following query In controller 在控制器中执行以下查询
$contain = array('Teacher' => array('User'));
$this->set('tutor', $this->Tutorsession->find('first',array(
'conditions' => array('Teacher.user_id' => $id),
'contain' => $contain
)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.