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 |
Результат | boolean | Cake\Datasource\EntityInterface | false if $entity could not be saved, otherwise it returns the saved entity |
public function saveAssociated(EntityInterface $entity, array $options = [])
{
$targetEntity = $entity->get($this->property());
$strategy = $this->saveStrategy();
$isEmpty = in_array($targetEntity, [null, [], '', false], true);
if ($isEmpty && $entity->isNew()) {
return $entity;
}
if ($isEmpty) {
$targetEntity = [];
}
if ($strategy === self::SAVE_APPEND) {
return $this->_saveTarget($entity, $targetEntity, $options);
}
if ($this->replaceLinks($entity, $targetEntity, $options)) {
return $entity;
}
return false;
}
/** * 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); }