/**
  * Test saving filtered parent associations.
  *
  * @return void
  */
 public function testSaveParentsFiltered()
 {
     $table = $this->getMockBuilder('Cake\\ORM\\Table')->setMethods(['table'])->getMock();
     $table->schema([]);
     $mockOne = $this->getMockBuilder('Cake\\ORM\\Association\\BelongsTo')->setMethods(['saveAssociated'])->setConstructorArgs(['Parents', ['sourceTable' => $table]])->getMock();
     $mockTwo = $this->getMockBuilder('Cake\\ORM\\Association\\BelongsTo')->setMethods(['saveAssociated'])->setConstructorArgs(['Categories', ['sourceTable' => $table]])->getMock();
     $this->associations->add('Parents', $mockOne);
     $this->associations->add('Categories', $mockTwo);
     $entity = new Entity();
     $entity->set('parent', ['key' => 'value']);
     $entity->set('category', ['key' => 'value']);
     $options = ['atomic' => true];
     $mockOne->expects($this->once())->method('saveAssociated')->with($entity, ['atomic' => true, 'associated' => ['Others']])->will($this->returnValue(true));
     $mockTwo->expects($this->never())->method('saveAssociated');
     $result = $this->associations->saveParents($table, $entity, ['Parents' => ['associated' => ['Others']]], $options);
     $this->assertTrue($result, 'Save should work.');
 }
Exemple #2
0
 /**
  * Performs the actual saving of an entity based on the passed options.
  *
  * @param \Cake\Datasource\EntityInterface $entity the entity to be saved
  * @param \ArrayObject $options the options to use for the save operation
  * @return \Cake\Datasource\EntityInterface|bool
  * @throws \RuntimeException When an entity is missing some of the primary keys.
  */
 protected function _processSave($entity, $options)
 {
     $primaryColumns = (array) $this->primaryKey();
     if ($options['checkExisting'] && $primaryColumns && $entity->isNew() && $entity->has($primaryColumns)) {
         $alias = $this->alias();
         $conditions = [];
         foreach ($entity->extract($primaryColumns) as $k => $v) {
             $conditions["{$alias}.{$k}"] = $v;
         }
         $entity->isNew(!$this->exists($conditions));
     }
     $mode = $entity->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE;
     if ($options['checkRules'] && !$this->checkRules($entity, $mode, $options)) {
         return false;
     }
     $options['associated'] = $this->_associations->normalizeKeys($options['associated']);
     $event = $this->dispatchEvent('Model.beforeSave', compact('entity', 'options'));
     if ($event->isStopped()) {
         return $event->result;
     }
     $saved = $this->_associations->saveParents($this, $entity, $options['associated'], ['_primary' => false] + $options->getArrayCopy());
     if (!$saved && $options['atomic']) {
         return false;
     }
     $data = $entity->extract($this->schema()->columns(), true);
     $isNew = $entity->isNew();
     if ($isNew) {
         $success = $this->_insert($entity, $data);
     } else {
         $success = $this->_update($entity, $data);
     }
     if ($success) {
         $success = $this->_associations->saveChildren($this, $entity, $options['associated'], ['_primary' => false] + $options->getArrayCopy());
         if ($success || !$options['atomic']) {
             $this->dispatchEvent('Model.afterSave', compact('entity', 'options'));
             $entity->clean();
             if (!$options['atomic'] && !$options['_primary']) {
                 $entity->isNew(false);
                 $entity->source($this->registryAlias());
             }
             $success = true;
         }
     }
     if (!$success && $isNew) {
         $entity->unsetProperty($this->primaryKey());
         $entity->isNew(true);
     }
     if ($success) {
         return $entity;
     }
     return false;
 }