I made myself a plugin for loading related content with beforeFind()
, so you could say that ContentPage/10
is similar to ConentNews/10
and Gallery/5
.
My table related_contents looks like:
id int(11)
source_table_name varchar(255)
target_table_name varchar(255)
source_table_id int(11)
target_table_id int(11)
My code in behavior:
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) {
$attachedTables = self::getAttachedTables(); //Array of ORM models which has this behavior
foreach ($attachedTables as $attachedTable) {
$options = [
'through' => 'RelatedContents',
'bindingKey' => 'RelatedContents.source_table_id',
'foreignKey' => 'RelatedContents.target_table_id',
'conditions' => [
'RelatedContents.source_table_name' => $this->_table->table(),
'target_table_name' => $attachedTable->table(),
]
];
$this->_table->belongsToMany($attachedTable->alias(), $options);
}
}
Now, when i try to find()
in my model, zero related entities are found with no error. What am i doing wrong?