我有两个表,列如下:守护者(id,student_no)和student(id,admission_no)。student_no是foreign to admision_no。学生与hasMany与守护者有联系,守护者与学生有belongsTo关联E 215。这是我的模特
学生
public $hasMany = array(
'Guardian' => array(
'className' => 'Guardian',
'foreignKey' => 'student_no',
'dependent' => true,
)
)守护者
public $belongsTo = array(
'Student' => array(
'className' => 'Student',
'foreignKey' => 'student_no',
)
)守护者控制器
public function view($id = null) {
if (!$this->Guardian->exists($id)) {
throw new NotFoundException(__('Invalid guardian'));
}
$options = array('conditions' => array('Guardian.' . $this->Guardian->primaryKey => $id));
$this->set('guardian', $this->Guardian->find('first', $options));
}守护者view.ctp (截断为只查看卫报模型中的相关学生)
<h3><?php echo __('Associated Students'); ?></h3>
<?php if (!empty($guardian['Student'])): ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<th><?php echo __('Admission No'); ?></th>
<th><?php echo __('First Name'); ?></th>
<th><?php echo __('Last Name'); ?></th>
<th><?php echo __('Gender'); ?></th>
<th><?php echo __('Date Of Birth'); ?></th>
<th><?php echo __('Join Date'); ?></th>
<th><?php echo __('Form'); ?></th>
<th><?php echo __('Student Class'); ?></th>
<th><?php echo __('Middle Name'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php foreach ($guardian['Student'] as $student): ?>
<tr>`
<td><?php echo $student['admission_no']; ?></td>
line(115) <td><?php echo $student['first_name']; ?></td>
<td><?php echo $student['last_name']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['date_of_birth']; ?></td>
<td><?php echo $student['join_date']; ?></td>
<td><?php echo $student['form']; ?></td>
<td><?php echo $student['student_class']; ?></td>
<td><?php echo $student['middle_name']; ?></td>
</tr>
<?php endforeach; ?>
</table>我可以使用上面类似的代码从学生视图中查看相关的家长详细信息,但是在监护人视图中,我得到了相关监护人的错误信息。
ERROR:Warning (2):非法字符串偏移“first_name”应用程序/视图/监护人/view.ctp,第115行
对于it.what下面的三行代码来说,问题出在哪里?
发布于 2014-10-26 12:43:45
做一些调试:debug($guardian)
在您的belongsTo组合中,守护者只能有一个学生,因此您正在迭代单个学生的列(因此,$student变量将是一个字符串,因此出现错误)。
另见Cookbook > Models > Associations > belongsTo
https://stackoverflow.com/questions/26573024
复制相似问题