0

親と子としましょう。親は perl スクリプトで作成されます。親は子がすべて同じ情報を取得する場所(編集用ではありません)であり、子はそれ固有の情報を保持します。Cakephp の命名規則に従って、それらは親の id フィールドと子の parent_id を介して接続されます。親テーブルが別のアプリケーション (Perl 経由で追加されたレコード) を介して更新されると、子に表示されますが、レコードが削除された場合、子は更新されません。テーブルを更新する(親で削除されたレコードを削除する)ためのcakephpの規則に従う方法はありますか?子の親テーブルに 'dependent' => true がありますか? それは子テーブルにある必要がありますか?または、テーブルはアプリの外部で更新されるため、それは問題ではありませんか?

他に何もなければ、定期的にテーブルをチェックする 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'
    ),
    );
}
?>
4

1 に答える 1

1

Here's a little find call you can use to get all orphaned child records:

$this->Drug->FrenchTranslation->find('all', array(
  'fields' => array('id'),
  'conditions' => array(
    'Drug.id' => null
  ),
  'contain' => array(
    'Drug' => array(
      'fields' => array('id')
    )
  )
));

This should call a SQL command like this

SELECT `FrenchTranslation`.`id`, `Drug`.`id` 
FROM `french_translations` AS FrenchTranslation
LEFT JOIN `drugs` AS `Drug` ON (`Drug`.`id` = `FrenchTranslation`.`drug_id`)
WHERE `Drug`.`id` IS NULL

This will return all orphaned child records, which you can then iterate through and delete. You will have to do it for each child.

于 2012-06-06T15:17:45.347 に答える