【发布时间】:2018-01-19 19:37:09
【问题描述】:
我的“Reservation”模型和“Profile”模型具有 hasAndBelongsToMany 关联。
这是我的预订模型。
class Reservation extends AppModel {
.
.
var $hasAndBelongsToMany = array(
'Profile' => array(
'className' => 'Profile',
'joinTable' => 'profiles_reservations',
'foreignKey' => 'reservation_id',
'associationForeignKey' => 'profile_id',
'unique' => true,
)
);
这是我的个人资料模型。
class Profile extends AppModel {
var $name = 'Profile';
}
这是我的控制器。
function prac3($lname, $fname) {
$profiles = $this->Profile->find('all', array(
'conditions' => array(
'Profile.lname LIKE' => $lname.'%',
'Profile.fname LIKE' => '%'.$fname.'%'
),
'order'=>array( 'Profile.created DESC' ),
));
$this->set('profiles', $profiles);
}
这是我的看法。
<?php
if($profiles) {
foreach($profiles as $key => $profile): ?>
<tr>
<td><?= $profile['Profile']['id'] ?></td>
<td><?= $profile['Profile']['lname'] ?></td>
<td><?= $profile['Profile']['fname'] ?></td>
<td><?= $profile['Profile']['home_phone'] ?></td>
</tr>
endforeach;
echo '</table>';
}
?>
我想使用 Profile 模型在视图中获取 ['Reservation']['name']。我怎样才能做到这一点?
【问题讨论】:
-
添加到配置文件模型 HABTM 关系
-
我以前也这样做过,但这造成了问题。当我添加新的预留和配置文件时,具有配置文件 ID 的行在 profiles_reservations 表中消失了。
标签: cakephp associations