cakephp2 无法显示来自 contain 的数据
cakephp2 can't display data from contain
我正在使用 cake 开发一个网络应用 php 2.
我在显示 2 table 秒的数据时遇到问题...
我的模特:
<?php
class Discipline extends AppModel{
public $hasMany = "Student";
}
?>
<?php
class Student extends AppModel{
public $belongsTo = array(
"Discipline" => array(
"className" => "Discipline",
"foreignKey" => "discipline_id"
)
);
}
?>
这是我的学生控制器:
<?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]
我习惯于在蛋糕上开发php 3,我对这个错误感到非常尴尬..如何从我的学生视角显示来自学科 table 的数据?
有什么想法吗?谢谢
编辑:我在我的 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']
我正在使用 cake 开发一个网络应用 php 2.
我在显示 2 table 秒的数据时遇到问题...
我的模特:
<?php
class Discipline extends AppModel{
public $hasMany = "Student";
}
?>
<?php
class Student extends AppModel{
public $belongsTo = array(
"Discipline" => array(
"className" => "Discipline",
"foreignKey" => "discipline_id"
)
);
}
?>
这是我的学生控制器:
<?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]
我习惯于在蛋糕上开发php 3,我对这个错误感到非常尴尬..如何从我的学生视角显示来自学科 table 的数据?
有什么想法吗?谢谢
编辑:我在我的 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']