我有兩個Myisam表,讓我們說父母和孩子。父是在perl腳本中創建的。父母是孩子獲取信息(不用於編輯)的地方,而孩子則擁有獨特的信息。遵循CakePHP命名約定,它們通過Parent的id字段和孩子的parent_id連接。當通過另一個應用程序(通過Perl添加一條記錄)更新父表時,它將顯示在Child中,但如果記錄被刪除,則Child不會更新。有沒有一種方法遵循cakephp約定來進行表更新(刪除Parent中刪除的記錄)? 我的孩子的父表中有'dependent'=> true?這是否需要在子表中?或者這是不重要的,因爲表格在應用程序之外更新?MySQL刪除只存在於子表/ cakephp更新中的記錄?
如果不出意外,我也許可以建立一個cron作業來定期檢查表,但我不知道如何找到/刪除子表中的記錄不再父表通過存在MYSQL。連接的組合和在哪裏這樣的n這樣的<>?所以我的問題是我可以做到這一點與cakephp如果表更新的應用程序之外?或者我會怎麼做與MySQL?
代碼:
<?php
class Drug extends AppModel {
var $name = 'Drug';
var $order = "Drug.generic ASC";
var $useDbConfig = 'default';
var $actsAs = array('ExtendAssociations', 'Containable');
var $hasOne = array(
'FrenchTranslation' => array(
'className' => 'FrenchTranslation',
'dependent' => true
),
'GermanTranslation' => array(
'className' => 'GermanTranslation',
'dependent' => true
),
'SpanishTranslation' => array(
'className' => 'SpanishTranslation',
'dependent' => true
)
);
}
?>
<?php
class FrenchTranslation extends AppModel {
var $name = 'FrenchTranslation';
var $validate = array(
'drug_id'=>array(
'The drug_id must be unique.'=>array(
'rule'=>'isUnique',
'message'=>'The Drug ID must be unique.',
'on'=>'create'
),
'The drug_id must be numeric.'=>array(
'rule'=>array('numeric'),
'message'=>'The Drug ID must be numeric.'
)
),
'id'=>array(
'The id must be unique.'=>array(
'rule'=>'isUnique',
'message'=>'The ID must be unique.',
'on'=>'create'
),
'The id must be numeric.'=>array(
'rule'=>array('numeric'),
'message'=>'The ID must be numeric.'
)
)
);
var $belongsTo = array(
'Drug' => array(
'className' => 'Drug',
'foreignKey' => 'drug_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User'=>array(
'className'=>'User',
'foreignKey'=>'user_id'
),
);
}
?>
你能展示你的模型關係嗎? – jeremyharris
@jeremyharris用我的模型代碼更新了帖子?如果這不是你的意思,讓我知道。 – Jonathan
這看起來不錯。你怎麼刪除它,用'$ this-> Drug-> delete()'?因爲那應該刪除所有相關的FrenchTranslation,GermanTranslation和SpanishTranslation記錄。 – jeremyharris