我正在开发一个web应用程序使用蛋糕php 2。
我有一个问题,而显示数据从2个表…
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"
)
);
}
?>
这是我的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);
}
}
我正在尝试使用这个视图显示学生的数据:
<?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>";
}
?>
但是我在这一行有一个问题:
echo "<td>".$v['Discipline']['designation']."</td>";
显示如下错误:
notice (8): Undefined index: designation [APP'View'Students'admin_index.ctp, line 47]
我习惯在cakephp 3上开发,我对这个错误感到非常尴尬。如何从学生视图中显示学科表中的数据?
你知道吗?Thx
编辑:我在我的StudentsController上做了一个调试,我找到了我想显示的数据:
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'**
)
)
)
)
我觉得你应该换个视角
改变:
foreach($student as $k => $value){
$v = current($value);
:
foreach($student as $k => $v){
$v = current($v);
和变化:
echo "<td>".$v['Discipline']['designation']."</td>";
echo "<td>".$value['Discipline']['designation']."</td>";
用第一个找到的数组覆盖变量$v,因此$v将是$v['Student']
我自己不会使用current进入CakePHP的数组,但使用$v['Student']到处