【问题标题】:Display a field from a 3rd table显示第三个表中的字段
【发布时间】:2014-07-25 20:32:16
【问题描述】:

我试图在关系中显示第三个表中的一个字段,在检查了这里的帖子和文档之后,我仍然卡住了。我有 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

如果在 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'
        )
    );

【问题讨论】:

  • 尝试先调试 $item。调试($项目);然后将结果粘贴到您的问题中,以便我们为您提供帮助。您可以在 quesry 之前添加控制器, $this->Tutorsession->recursive = 2;通过结果,您可以看到如何访问它。
  • 数组非常大,所以我不知道如何将它放在这里但是它没有显示用户表中的任何内容。

标签: php mysql cakephp


【解决方案1】:

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

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

【讨论】:

  • 调试($tutor);还没有显示任何用户表?我喜欢代码,但我仍然无法显示用户名,未定义索引:用户回显“”。 $tutor['User']['username'].'';
  • 我在每个模型中都添加了这条线,但什么也没有!同样的错误。它只是讨厌从第三张表中显示此字段。 $tutor['User']['username'] //u定义索引用户
  • @ajt: 用于调试,请在查找中注释条件后尝试。让我知道发生了什么。想看看,有你的教师数据与 user_id。
  • 这是一个更好的解决方案,因为您不使用递归 $contain = array('Teacher' => array('User')); $this->set('tutor', $this->Tutorsession->find('first',array('conditions' => array('Teacher.user_id' => $id), 'contain' => $contain )));
【解决方案2】:

试试这个:

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

【讨论】:

  • 我尝试使用调试,但看不到任何用户表,我尝试了 abobe 但错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“包含”附近使用正确的语法
  • 也在 where 子句 'contain' $this->set( 'tutor', $this->Tutorsession->find( 'first', array( 'conditions' => array ('Teacher.user_id' => $id), 'contain' => 'User.username' ) ) );
  • public $actsAs = array('Containable');我在用户模型中添加了这个,没有对包含的错误进行任何更改。我真的很难从第三张桌子上得到一个简单的字段
  • 已解决,我需要引用字段 echo ''。 $tutor['Teacher']['User']['username'].'';并将可包含的内容添加到模型中
  • 将递归设置为-1会使事情再次失败,所以我不知道如何避免递归和包含。
【解决方案3】:
$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'
                  )
              )

确保您的模型正确

【讨论】:

  • 我没有像您在调试中那样获得用户,我如何在这个小区域添加我的调试?除了跨 3 个表获取数据之外,我的模型工作正常。用户表与辅导表没有直接关系。包含用户收到我无法修复的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
相关资源
最近更新 更多