【问题标题】:cakephp hasAndBelongsToManycakephp hasAndBelongsToMany
【发布时间】: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


【解决方案1】:

更新您的个人资料类

    class Profile extends AppModel  {
         var $name = 'Profile';

         var $hasAndBelongsToMany = array(
             'Reservation' => array(
                 'className' => 'Reservation',
                 'joinTable' => 'profiles_reservations',
                 'foreignKey' => 'profile_id',
                 'associationForeignKey' => 'reservation_id',
                 'unique' => true, // More about update below
         )
    }

在你的find() 方法中应该使用递归:

$profiles = $this->Profile->find('all', array(
        //...
        'recursive' => 2,
));

或可包含的行为:

$this->Profile->Behaviors->load('Containable');  

$profiles = $this->Profile->find('all', array(
        //...
        'contain' => array(
                'Reservation',
        ),
        'recursive' => -1,
));

您的保留将在数组中,如$profiles['Reservation'][n]['name']

另外,在您的评论中您写道“当我添加新的预订和个人资料时,具有个人资料 ID 的行在 profiles_reservations 表中消失了。
因为您在$hasAndBelongsToMany 属性中使用'unique' =&gt; true。食谱上说:

如果为true(默认值),CakePHP 将首先删除外键表中现有的关系记录,然后再插入新的关系记录。更新时需要重新传递已有的关联。

见:https://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

【讨论】:

  • 那么如果我想保存所有记录,是否需要使用'unique' => 'keepExisting'?
  • 阅读文档。我不知道你的想法是什么:)
【解决方案2】:

试试这个

class ReservationsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Profiles', [
            'joinTable' => 'reservation_profiles',
        ]);
    }
}
class ProfilesTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Reservations', [
            'joinTable' => 'reservation_profiles',
        ]);
    }
}

然后如您所见,ReservationsTable 是使用reservation_profiles 表将Reservation 与Profiles 相关联的模型,Profiles 也这样做。您不需要为 reservation_profiles 建立模型,但您的数据库中必须有此表,我建议您使用迁移来创建它们。最后在您的控制器中,这可能在您调用的 ReservationController 中

$this->Reservation->Profiles->find()->where(['condition'=> 'param']);

这可能会解决你的问题,解释($this)指的是Controller类,->REservation指的是模型Reservation -> Profiles指的是相关模型到类Reservation模型,->find()... 指到执行的查询。如果您需要更多https://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associations

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多