doukundong9652 2015-08-03 14:03
浏览 83
已采纳

cakephp2无法显示来自contains的数据

I am developing a web app using cake php 2.

I have an issue while displaying data from 2 tables...

My models :

<?php 

    class Discipline extends AppModel{

        public $hasMany = "Student";
    }

?>

<?php 

    class Student extends AppModel{

        public $belongsTo = array(
            "Discipline" => array(
                "className" => "Discipline",
                "foreignKey" => "discipline_id"
            )      
        );

    }

?>

Here is my studentscontroller :

<?php
class StudentsController extends AppController{

    function admin_index(){
        if($this->request->is('put') || $this->request->is('post')){
            $student = $this->request->data['Student'];
            if($this->Student->save($this->request->data)){
                $this->Session->setFlash("L'eleve a bien été modifié","notif");
            } 
        }
        $d['student'] = $this->Student->find('all',array(
            'contain' => "Discipline"
        )); 
        $this->set($d);
    }
}

I am trying to display student's data using this view :

<?php

foreach($student as $k => $v){ 
    $v = current($v);

    echo "<td>Action</td>";
    echo "<td>Label</td>";
    echo "<td>".$v['nom']." ".$v['prenom']."</td>"; 
    echo "<td>".$v['sexe']."</td>"; 
    echo "<td>".$v['naissance']."</td>"; 
    echo "<td>".$v['Discipline']['designation']."</td>"; 
    echo "<td>".$v['comite']."</td>"; 
    echo "<td>".$v['classe']."</td>"; 
    echo "<td>".$v['elite']."</td>"; 
    echo "<td>".$v['alerte']."</td>"; 
    echo "<td>".$v['quota1']."</td>"; 
    echo "<td>".$v['quota2']."</td>"; 
    echo "<td>".$v['total']."</td>"; 
    echo "<td>Pris</td>"; 
    echo "<td>Restant</td>"; 
    echo "<td>Supp</td>"; 


}

?> 

But i have an issue on this line :

 echo "<td>".$v['Discipline']['designation']."</td>";

It says this error :

notice (8): Undefined index: designation [APP\View\Students\admin_index.ctp, line 47]

I am used to develop on cakephp 3 and I am pretty embarassed with that error.. what to do to display data from Disciplines table from my student view ?

Any idea ? Thx

EDIT: I did a debug on my StudentsController, and I found the data I wanna display :

array(
    'student' => array(
        (int) 0 => array(
            'Student' => array(
                'id' => '2',
                'prenom' => 'Jean',
                'nom' => 'Michel',
                'sexe' => '1',
                'naissance' => '2015-08-02',
                'age' => '12',
                'classe' => '1',
                'discipline_id' => '1',
                'comite' => 'test',
                'categorie' => 'test',
                'elite' => true,
                'alerte' => 'test',
                'quota1' => '12',
                'quota2' => '12',
                'total' => '24',
                'note' => 'tete'
            ),
            'Discipline' => array(
                **'id' => '1',
                'designation' => 'Alpin'**
            )
        )
    )
)
  • 写回答

1条回答 默认 最新

  • dongpangzan6425 2015-08-03 14:46
    关注

    i think you should change the view

    Change:

    foreach($student as $k => $value){ 
        $v = current($value);
    

    to:

    foreach($student as $k => $v){ 
        $v = current($v);
    

    and change:

    echo "<td>".$v['Discipline']['designation']."</td>"; 
    

    to

    echo "<td>".$value['Discipline']['designation']."</td>";
    

    You overwrite the variable $v with the first found array, so $v would be $v['Student']

    I for myself won't use current to go inside a array for CakePHP, but use $v['Student'] everywhere

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟