请帮助模型协会。架构看起来像:
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 无法添加或更新子行:外键约束失败(schema
. translation_typeofs
, CONSTRAINT translation_typeofs_typeof_buildings_fkid1
FOREIGN KEY ( typeof_id
) REFERENCES typeof_buildings
( id
) ON DELETE NO ACTION ON UPDATE NO ACTION)
我认为这与模型关联有关,但找不到解决方案。