显示第3个表格中的字段

时间:2014-06-05 01:25:34

标签: php mysql cakephp

我试图在关系中显示第3张表中的字段,在检查这里的帖子和文档之后,我仍然卡住了。我有3个模型都以某种方式相关。我在这里发现了类似的帖子,但我仍然没有得到它的工作。我正在学习,如果我在文档的某个地方错过了这个对不起,但我已经阅读了很多,并且已经尝试了很多。我现在猜太多,所以我需要帮助。

 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

tutorsession会自动从教师表中获取行,但不会自动获取用户表,而不是在findall上进行模型设置。

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

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

3 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

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

确保您的模型正确

答案 2 :(得分:0)

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

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