attachTo() public method

The options array accept the following keys: - includeFields: Whether to include target model fields in the result or not - foreignKey: The name of the field to use as foreign key, if false none will be used - conditions: array with a list of conditions to filter the join with - fields: a list of fields in the target table to include in the result - type: The type of join to be used (e.g. INNER)
public attachTo ( Query $query, array $options = [] ) : void
$query Cake\ORM\Query the query to be altered to include the target table data
$options array Any extra options or overrides to be taken in account
return void
 /**
  * Tests that attaching an association to a query will trigger beforeFind
  * for the target table
  *
  * @return void
  */
 public function testAttachToBeforeFindExtraOptions()
 {
     $query = $this->getMock('\\Cake\\ORM\\Query', ['join', 'select'], [null, null]);
     $config = ['sourceTable' => $this->article, 'targetTable' => $this->tag];
     $table = TableRegistry::get('ArticlesTags');
     $association = new BelongsToMany('Tags', $config);
     $listener = $this->getMock('stdClass', ['__invoke']);
     $this->tag->getEventManager()->attach($listener, 'Model.beforeFind');
     $opts = ['something' => 'more'];
     $listener->expects($this->once())->method('__invoke')->with($this->isInstanceOf('\\Cake\\Event\\Event'), $this->isInstanceOf('\\Cake\\ORM\\Query'), $opts, false);
     $listener2 = $this->getMock('stdClass', ['__invoke']);
     $table->getEventManager()->attach($listener2, 'Model.beforeFind');
     $listener2->expects($this->once())->method('__invoke')->with($this->isInstanceOf('\\Cake\\Event\\Event'), $this->isInstanceOf('\\Cake\\ORM\\Query'), [], false);
     $association->attachTo($query, ['queryBuilder' => function ($q) {
         return $q->applyOptions(['something' => 'more']);
     }]);
 }