我的数据库
学生桌
ID NAME PARENT_ID (VARCHAR)
1 JOHN NULL
2 MATT NULL
3 PETER 1,2
在表格列表中显示
ID NAME MENTOR NAME
1 JOHN NULL
2 MATT NULL
3 PETER JOHN, MATT
在为每个循环创建一个在表格上显示数据列表时,如何显示“John and Matt 而不是“1,2”?
我在 $belongTo 的 Student Class 中有一个 StudentExtra 来显示导师姓名。但是,此方法仅显示一个导师姓名。
模型
class Student extends AppModel
{
// Mainly for PHP4 users
var $name = 'Student';
var $hasMany = array('Order','Test');
var $belongsTo = array
(
'StudentExtra'=>array
(
'className' => 'Student',
'foreignKey' => 'parent_id'
),'School','Classroom'
);
控制器
$this->paginate = array(
'limit' => 10,
'order' => array('Student.name' => 'ASC'));
$this->set('students', $this->paginate('Student'));
看法
<table>
<?php foreach ($students as $student){ ?>
<tr>
<td>
<?php $student['Student']['id'] ?>
</td>
</tr>
<tr>
<td>
<?php $student['Student']['name'] ?>
</td>
</tr>
<tr>
<td>
<?php $student['Student']['parent_id'] ?>
</td>
</tr>
<?php } ?>
</table>
结果
Array
(
[0] => Array
(
[Student] => Array
(
[id] => 1190
[customers_gender] => M
[resumetest] =>
[created] => 2013-04-01 19:29:08
[modified] => 2013-04-01 19:29:08
[customers_name] => aaa aaa
[customers_dob] => 1970-01-01
[customers_roles] => student
[customers_types] => student
[customers_parent_id] => 1055,1056
[school_id] => 40
[classroom_id] => 96
)
[StudentExtra] => Array
(
[id] => 1055
[customers_gender] => F
[created] => 2013-03-28 11:14:25
[modified] => 2013-03-28 11:14:25
[customers_name] => Mich
[customers_dob] => 1970-01-01
[customers_roles] => teacher
[customers_types] => teacher
[customers_parent_id] =>
[school_id] => 40
[classroom_id] => 96
)
[School] => Array
(
[id] => 40
[name] => Primary School
[description] =>
[created] => 2013-03-28 10:47:05
[modified] => 2013-03-28 10:47:05
)
[Classroom] => Array
(
[id] => 96
[name] => 30G
[year_level] => 3
[description] =>
[created] => 2013-03-28 10:58:25
[modified] => 2013-03-28 10:58:25
[school_id] => 40
)
[Order] => Array
(
)
[Test] => Array
(
)
)