2012-08-09 38 views
2

请帮助模型关联。 架构的样子: Data model schemaCakePHP 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无法添加或更新子行:外键约束失败(schematranslation_typeofs,约束translation_typeofs_typeof_buildings_fkid1外键(typeof_id)参考文献typeof_buildingsid)ON DELETE NO ACTION ON UPDATE NO ACTION)

我认为这是与模型关联的东西,但无法找到解决方案。

+0

您正试图存储'typeof_id'让我们说'typeof_id = 3',那么它可能不存在于foreignKey表中。 – 2012-08-09 04:18:04

+0

感谢您的回答,我试图添加的所有id都存在于foreignKey表中。 – 2012-08-09 04:37:22

回答

2

由于typeof_id充当外键,取决于typeof_type提供给它。所以不应该有任何数据库的外键约束可以适用。

因此,您可以做的是:“使用PhpMyAdmin从数据库中删除foreignKey约束,并让我们使用CakePHP”“来处理它。请问如果它不适合你。

+1

非常感谢,它的作品。非常简单! :) – 2012-08-09 04:49:37