显示第三个表格中的字段


Display a field from a 3rd table

我正试图显示关系中第三个表中的字段,在检查了这里的帖子和文档后,我仍然被卡住了。我有三个模型,在某种程度上都是相关的。我在这里找到了类似的帖子,但我仍然没有让它发挥作用。我很抱歉,如果我在文档中的某个地方错过了这个,但我已经读了很多,也尝试了很多。我现在对此猜测太多了,所以我需要帮助。

 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

在findall上设置模型的情况下,教程会话会自动从教师表中获取行,但不会从用户表中获取。

我想显示用户表中的用户名。我显示教程会话表,然后我可以显示具有模型关联的教师表,但我不能从教师表转到用户表,从用户id中获取用户名作为公共字段。我已经检查了下面的文档,我不确定为什么我的代码不能显示用户表中的用户名。

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.htmlhttp://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'
        )
    );

在模型中添加可包含的行为

public $actsAs = array('Containable');

在控制器中执行以下查询

$contain = array('Teacher' => array('User'));
$this->set('tutor', $this->Tutorsession->find('first',array(
        'conditions' => array('Teacher.user_id' => $id),
        'contain' => $contain
    )));

试试这个:

$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();

您的结果类似:

'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'
                  )
              )

确保您的模型是正确的