I have a Files Model, and Multiple (currently 3) different other Models (Article, Job, Event) that can all have files, that are stored in the Files Model.
The problem is that when i generate the tables via the CLI-Tool (./doctrine build-all-reload), i get this error message:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot
add or update a child row: a foreign key constraint fails
(`my_database/articles`, CONSTRAINT `articles_id_files_target_id`
FOREIGN KEY (`id`) REFERENCES `files` (`target_id`))
File is defined as (No relations are defined in this Model defined):
columns:
id:
primary: true
autoincrement: true
type: integer(4)
target_id: integer(4)
filename: string(255)
[...]
All 4 Models have this relation-definition:
relations:
Files:
type: many
class: File
local: id
foreign: target_id
This is the Php-Code that Doctrine generates (BaseFile.php):
public function setUp()
{
parent::setUp();
$this->hasOne('Publication', array(
'local' => 'target_id',
'foreign' => 'id'));
$this->hasOne('Event', array(
'local' => 'target_id',
'foreign' => 'id'));
$this->hasOne('Article', array(
'local' => 'target_id',
'foreign' => 'id'));
$this->hasOne('Job', array(
'local' => 'target_id',
'foreign' => 'id'));
}
I understand why this happens (The Constraints can not be setup for multiple tables), but have no idea how i could solve this problem without mutltiple file tables or an association table.
Is there a way to tell Doctrine that it should not create the relations in the File model?
Any good ideas?