saveAssociated() public method

When using the 'append' strategy, this function will only create new links between each side of this association. It will not destroy existing ones even though they may not be present in the array of entities to be saved. When using the 'replace' strategy, existing links will be removed and new links will be created in the joint table. If there exists links in the database to some of the entities intended to be saved by this method, they will be updated, not deleted.
See also: Cake\ORM\Table::save()
See also: Cake\ORM\Association\BelongsToMany::replaceLinks()
public saveAssociated ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean | Cake\Datasource\EntityInterface
$entity Cake\Datasource\EntityInterface an entity from the source table
$options array options to be passed to the save method in the target table
return boolean | Cake\Datasource\EntityInterface false if $entity could not be saved, otherwise it returns the saved entity
示例#1
0
 /**
  * Test that saveAssociated() ignores non entity values.
  *
  * @return void
  */
 public function testSaveAssociatedOnlyEntitiesAppend()
 {
     $connection = ConnectionManager::get('test');
     $mock = $this->getMockBuilder('Cake\\ORM\\Table')->setMethods(['saveAssociated', 'schema'])->setConstructorArgs([['table' => 'tags', 'connection' => $connection]])->getMock();
     $mock->primaryKey('id');
     $config = ['sourceTable' => $this->article, 'targetTable' => $mock, 'saveStrategy' => BelongsToMany::SAVE_APPEND];
     $entity = new Entity(['id' => 1, 'title' => 'First Post', 'tags' => [['tag' => 'nope'], new Entity(['tag' => 'cakephp'])]]);
     $mock->expects($this->never())->method('saveAssociated');
     $association = new BelongsToMany('Tags', $config);
     $association->saveAssociated($entity);
 }