2

モデルの関連付けを手伝ってください。スキーマは次のようになります。 データモデルスキーマ

translation_typeofs.typeof_idはtypeof_[name]外部キーです

translation_typeofs.typeof_typeコンテンツリレーションテーブルの名前の一部(日、色、建物、または部屋)。

TypeofDaysモデル:

class TypeofDay extends AppModel {
    public $name = 'TypeofDay';

    public $hasMany = array(
        'GalleriesTypeofDay' => array(
            'className'  => 'GalleriesTypeofDay',
            'dependent' => true
        ),
        'TranslationTypeof' => array(
            'className'  => 'TranslationTypeof',
            'conditions' => array('TranslationTypeof.typeof_type' => 'days'),
            'dependent' => true
        )
    );
}

TranslationTypeofモデル:

class TranslationTypeof extends AppModel {
    public $name = 'TranslationTypeof';

    public $belongsTo = array(
        'Language' => array(
            'className'    => 'Language',
            'foreignKey' => 'language_id',
        ),
        'TypeofBuilding' => array(
            'className'  => 'TypeofBuilding',
            'foreignKey' => 'typeof_id',
            'conditions' => array('TranslationTypeof.typeof_type' => 'buildings')
        ),
        'TypeofColour' => array(
            'className'  => 'TypeofColour',
            'foreignKey' => 'typeof_id',
            'conditions' => array('TranslationTypeof.typeof_type' => 'colours')
        ),
        'TypeofRoom' => array(
            'className'  => 'TypeofRoom',
            'foreignKey' => 'typeof_id',
            'conditions' => array('TranslationTypeof.typeof_type' => 'rooms')
        ),
        'TypeofDay' => array(
            'className'  => 'TypeofDay',
            'foreignKey' => 'typeof_id',
            'conditions' => array('TranslationTypeof.typeof_type' => 'days')
        )
    );
}

データを保存しようとすると

$this->TranslationTypeof->saveAll(array(
    'typeof_type'   => $this->data['type_name'],
    'typeof_id'     => $this->data['type_id'],
    'language_id'   => $languageId,
    'text'          => $translation
));

エラー500(内部サーバーエラー)が発生します

エラーログ:

エラー:[PDOException] SQLSTATE [23000]:整合性制約違反:1452子行を追加または更新できません:外部キー制約が失敗します(schematranslation_typeofs、CONSTRAINT translation_typeofs_typeof_buildings_fkid1FOREIGN KEY(typeof_id)REFERENCES typeof_buildingsid)ON DELETE NO ACTION ON UPDATE NO ACTION)

モデルの関連付けがあると思いますが、解決策が見つかりません。

4

1 に答える 1

2

typeof_id提供されたものに応じて、外部キーとして機能しますtypeof_type。したがって、データベースの外部キー制約を適用することはできません。

したがって、実行できることは、「PhpMyAdminを使用してデータベースからforeignKey制約を削除し、CakePHPで処理できるようにする」です。それがうまくいかなかったかどうか尋ねてください。

于 2012-08-09T04:42:42.500 に答える