我的数据库
学生表
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和Matt而不是”1,2“?
我在$ belongsTo中的学生班中有一个StudentExtra来显示导师姓名。但是,此方法仅显示一个指导者名称。
MODEL
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'
);
CONTROLLER
$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>
RESULT
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
(
)
)
答案 0 :(得分:2)
其他cakephp方式如下。
您还可以将关联设置为学生模型,如下所示。
在学生模型中添加以下代码
var $belongsTo = array
(
'StudentExtra'=>array
(
'className' => 'Student',
'foreignKey' => 'parent_id'
)
);
在控制器中
$this->set('students', $this->paginate('Student'));
现在只需执行pr($students); exit();
即可在视图中或控制器功能结束时测试结果。
您将看到StudentExtra的额外附加记录数组,这就是您所需要的。
现在在视图文件中显示如下。
<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['StudentExtra']['name'] ?>
</td>
</tr>
<?php } ?>
</table>
回答更新回复代码
$this->paginate = array
(
'joins'=>array
(
array
(
'table'=>'students',
'alias'=>'StudentExtra',
'type' =>'inner',
'conditions' =>array('StudentExtra.id IN (Student.parent_id)')
)
),
'limit'=> 10,
'order' => array('Student.name' => 'ASC')
);
$this->set('students', $this->paginate('Student'));
debug($this->paginate('Student'));
exit;
答案 1 :(得分:1)
创建学生模型:
class Students extends AppModel {
public $useTable = false;
public function getParentName($id) {
$id = (int) $id;
$sql = "
SELECT NAME FROM `students` WHERE ID = (
SELECT PARENT_ID FROM `students` WHERE ID = $id
)
");
return $this->query($sql);
}
}
然后在你的控制器中:
$this->Students->getParentName(2); // in this case for Peter