belongsToMany() public method

Target table can be inferred by its name, which is provided in the first argument, or you can either pass the class name to be instantiated or an instance of it directly. The options array accept the following keys: - className: The class name of the target table object. - targetTable: An instance of a table object to be used as the target table. - foreignKey: The name of the field to use as foreign key. - targetForeignKey: The name of the field to use as the target foreign key. - joinTable: The name of the table representing the link between the two - through: If you choose to use an already instantiated link table, set this key to a configured Table instance containing associations to both the source and target tables in this association. - dependent: Set to false, if you do not want junction table records removed when an owning record is removed. - cascadeCallbacks: Set to true if you want CakePHP to fire callbacks on cascaded deletes. If false the ORM will use deleteAll() to remove data. When true join/junction table records will be loaded and then deleted. - conditions: array with a list of conditions to filter the join with. - sort: The order in which results for this association should be returned. - strategy: The strategy to be used for selecting results Either 'select' or 'subquery'. If subquery is selected the query used to return results in the source table will be used as conditions for getting rows in the target table. - saveStrategy: Either 'append' or 'replace'. Indicates the mode to be used for saving associated entities. The former will only create new links between both side of the relation and the latter will do a wipe and replace to create the links between the passed entities when saving. - strategy: The loading strategy to use. 'select' and 'subquery' are supported. - finder: The finder method to use when loading records from this association. Defaults to 'all'. This method will return the association object that was built.
public belongsToMany ( string $associated, array $options = [] ) : BelongsToMany
$associated string the alias for the target table. This is used to uniquely identify the association
$options array list of options to configure the association definition
return Cake\ORM\Association\BelongsToMany
Example #1
0
 /**
  * Generate reciprocal associations as necessary.
  *
  * Generates the following associations:
  *
  * - target hasMany junction e.g. Articles hasMany ArticlesTags
  * - target belongsToMany source e.g Articles belongsToMany Tags.
  *
  * You can override these generated associations by defining associations
  * with the correct aliases.
  *
  * @param \Cake\ORM\Table $junction The junction table.
  * @param \Cake\ORM\Table $source The source table.
  * @param \Cake\ORM\Table $target The target table.
  * @return void
  */
 protected function _generateTargetAssociations($junction, $source, $target)
 {
     $junctionAlias = $junction->alias();
     $sAlias = $source->alias();
     if (!$target->association($junctionAlias)) {
         $target->hasMany($junctionAlias, ['targetTable' => $junction, 'foreignKey' => $this->targetForeignKey()]);
     }
     if (!$target->association($sAlias)) {
         $target->belongsToMany($sAlias, ['sourceTable' => $target, 'targetTable' => $source, 'foreignKey' => $this->targetForeignKey(), 'targetForeignKey' => $this->foreignKey(), 'through' => $junction, 'conditions' => $this->conditions()]);
     }
 }
Example #2
0
 /**
  * Tests that BelongsToMany() creates and configures correctly the association
  *
  * @return void
  */
 public function testBelongsToMany()
 {
     $options = ['foreignKey' => 'thing_id', 'joinTable' => 'things_tags', 'conditions' => ['b' => 'c'], 'sort' => ['foo' => 'asc']];
     $table = new Table(['table' => 'authors', 'connection' => $this->connection]);
     $belongsToMany = $table->belongsToMany('tag', $options);
     $this->assertInstanceOf('Cake\\ORM\\Association\\BelongsToMany', $belongsToMany);
     $this->assertSame($belongsToMany, $table->association('tag'));
     $this->assertEquals('tag', $belongsToMany->name());
     $this->assertEquals('thing_id', $belongsToMany->foreignKey());
     $this->assertEquals(['b' => 'c'], $belongsToMany->conditions());
     $this->assertEquals(['foo' => 'asc'], $belongsToMany->sort());
     $this->assertSame($table, $belongsToMany->source());
     $this->assertSame('things_tags', $belongsToMany->junction()->table());
 }