CakePHPでのHABTMの関係に問題があります。
私はそのような2つのモデルを持っています:DepartmentHABTM Location。1つの大企業には多くの建物があり、各建物は限られた数のサービスを提供しています。各建物には独自のWebページもあるため、HABTMの関係自体に加えて、各HABTM行にはurl、ユーザーがアクセスして、関心のあるサービスとその建物での動作に関する追加情報を見つけることができるフィールドもあります。に興味がある。
私は次のようにモデルを設定しました:
<?php
class Location extends AppModel {
var $name = 'Location';
var $hasAndBelongsToMany = array(
'Department' => array(
'with' => 'DepartmentsLocation',
'unique' => true
)
);
}
?>
<?php
class Department extends AppModel {
var $name = 'Department';
var $hasAndBelongsToMany = array(
'Location' => array(
'with' => 'DepartmentsLocation',
'unique' => true
)
);
}
?>
<?php
class DepartmentsLocation extends AppModel {
var $name = 'DepartmentsLocation';
var $belongsTo = array(
'Department',
'Location'
);
// I'm pretty sure this method is unrelated. It's not being called when this error
// occurs. Its purpose is to prevent having two HABTM rows with the same location
// and department.
function beforeSave() {
// kill any existing rows with same associations
$this->log(__FILE__ . ": killing existing HABTM rows", LOG_DEBUG);
$result = $this->find('all', array("conditions" =>
array("location_id" => $this->data['DepartmentsLocation']['location_id'],
"department_id" => $this->data['DepartmentsLocation']['department_id'])));
foreach($result as $row) {
$this->delete($row['DepartmentsLocation']['id']);
}
return true;
}
}
?>
コントローラーはまったく面白くありません。
問題:
の名前を編集すると、それにリンクされていたLocationすべてのが空のURLで再作成されます。モデルはuniqueがtrueであると指定しているため、これにより、すべての新しい行が古い行を上書きし、基本的にすべてのURLが破棄されます。DepartmentsLocationLocation
私は2つのことを知りたいです:私はこれを止めることができますか?もしそうなら、どのように?
そして、あまり技術的ではなく、より気まぐれなメモで:なぜこれが起こるのですか?Cakeでフィールドを編集すると、非常に多くの問題が発生するのは奇妙に思えます。phpMyAdminを簡単に調べて、Locationそこで名前を編集し、期待どおりの結果を得ることができる場合です。行のフィールドを編集しているだけなのに、CakePHPがHABTMデータに触れるのはなぜですか?外部キーすらありません!