请帮助模型关联。 架构的样子: CakePHP 2 - 许多表的外键
translation_typeofs.typeof_id是typeof_ [名]关系表(天,颜色,建筑物或房间)的名称外键
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
,约束translation_typeofs_typeof_buildings_fkid1
外键(typeof_id
)参考文献typeof_buildings
(id
)ON DELETE NO ACTION ON UPDATE NO ACTION)
我认为这是与模型关联的东西,但无法找到解决方案。
您正试图存储'typeof_id'让我们说'typeof_id = 3',那么它可能不存在于foreignKey表中。 – 2012-08-09 04:18:04
感谢您的回答,我试图添加的所有id都存在于foreignKey表中。 – 2012-08-09 04:37:22