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 无法添加或更新子行:外键约束失败(schema. translation_typeofs, CONSTRAINT translation_typeofs_typeof_buildings_fkid1FOREIGN KEY ( typeof_id) REFERENCES typeof_buildings( id) ON DELETE NO ACTION ON UPDATE NO ACTION)

我认为这与模型关联有关,但找不到解决方案。

4

1 回答 1

2

就像提供给它typeof_id的外键一样。typeof_type所以不应该有任何数据库foreignKey约束可以适用。

所以你可以做的是:“使用 PhpMyAdmin 从数据库中删除 foreignKey 约束,然后用 CakePHP 处理它”。请询问它是否不适合您。

于 2012-08-09T04:42:42.500 回答