Provides a methods to handle "many to many" relation between tables.
Important notes:
- Please update the database after defining the new relation.
- Currently the
eager
load is not supported. - The relation table name is consists of the original table name and related table name, e.g. tl_table_one_table_two.
- If you delete a record in the related table then the relation tables are automatically updated.
<?php
$GLOBALS['TL_DCA']['tl_table_one']['fields']['my_field']['relation'] = array
(
'type' => 'haste-ManyToMany',
'load' => 'lazy',
'table' => 'tl_table_two', // the related table
'reference' => 'id', // current table field (optional)
'referenceSql' => "int(10) unsigned NOT NULL default '0'", // current table field sql definition (optional)
'field' => 'id', // related table field (optional)
'fieldSql' => "int(10) unsigned NOT NULL default '0'", // related table field sql definition (optional)
'relationTable' => '', // custom relation table name (optional)
'forceSave' => true // false by default. If set to true it does not only store the values in the relation tables but also the "my_relation" field
);
The model class must extend \Haste\Model\Model.
<?php
class TableOneModel extends \Haste\Model\Model
{
// ...
}
Then call it as usual
$objRelated = $objModel->getRelated('my_field');
You can also fetch the related or reference values manually:
$arrRelated = static::getRelatedValues('tl_table_one', 'my_field', 123);
$arrReference = static::getReferenceValues('tl_table_one', 'my_field', array(1, 2, 3));