Reputation: 2092
Please help with model associations.
Schema looks like:
translation_typeofs.typeof_id is typeof_[name] foreign key
translation_typeofs.typeof_type content part of name of relation table (days, colours, buildings or rooms).
TypeofDays model:
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 model:
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')
)
);
}
When I try to save data
$this->TranslationTypeof->saveAll(array(
'typeof_type' => $this->data['type_name'],
'typeof_id' => $this->data['type_id'],
'language_id' => $languageId,
'text' => $translation
));
I'm getting error 500 (Internal Server Error)
Error log:
Error: [PDOException] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (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)
I'm think that it's something with model association, but can't find the solution.
Upvotes: 1
Views: 1075
Reputation: 5464
As typeof_id
is acting as a foreign key depending on the typeof_type
provided to it. So there should not be any database foreignKey constraint can be applicable.
So what you can do is: "Delete the foreignKey constraint from the database using PhpMyAdmin and lets handle it with CakePHP". Kindly ask if it not worked for you.
Upvotes: 2