Skip to content

Commit

Permalink
Only trying to save associatiations if they are marked
Browse files Browse the repository at this point in the history
as dirty
  • Loading branch information
lorenzo committed Nov 28, 2013
1 parent fd8ea89 commit 86a1fa8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions Cake/ORM/Table.php
Expand Up @@ -972,10 +972,11 @@ protected function _processSave($entity, $options) {
if ($success) {
$success = $this->_saveAssociations($children, $entity, $originalOptions);
if ($success || !$options['atomic']) {
$success = $entity;
$entity->clean();
$event = new Event('Model.afterSave', $this, compact('entity', 'options'));
$this->getEventManager()->dispatch($event);
$entity->isNew(false);
$success = $entity;
}
}

Expand All @@ -995,7 +996,7 @@ protected function _sortAssociationTypes($assocs) {
$msg = __d('cake_dev', '%s is not associated to %s', $this->alias(), $assoc);
throw new \InvalidArgumentException($msg);
}

if ($association->isOwningSide()) {
$children[] = $assoc;
} else {
Expand All @@ -1010,9 +1011,16 @@ protected function _saveAssociations($assocs, $entity, array $options) {
return $entity;
}

unset($options['associated']);

foreach ($assocs as $alias) {
$association = $this->association($alias);
unset($options['associated']);
$property = $association->property();

if (!$entity->dirty($property)) {
continue;
}

if (!$association->save($entity, $options)) {
return false;
}
Expand Down Expand Up @@ -1049,7 +1057,6 @@ protected function _insert($entity, $data) {
if ($id !== null) {
$entity->set($primary, $id);
}
$entity->clean();
$success = $entity;
}
$statement->closeCursor();
Expand Down Expand Up @@ -1105,7 +1112,6 @@ protected function _update($entity, $data) {

$success = false;
if ($statement->rowCount() > 0) {
$entity->clean();
$success = $entity;
}
$statement->closeCursor();
Expand Down
2 changes: 1 addition & 1 deletion Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -2201,7 +2201,7 @@ public function testSaveHasOneWithValidationErrorNonAtomic() {
* @group save
* @return void
*/
public function testsSaveBelongsToWithValidationErrorNotAtomic() {
public function testSaveBelongsToWithValidationErrorNotAtomic() {
$entity = new \Cake\ORM\Entity([
'title' => 'A Title',
'body' => 'A body'
Expand Down

0 comments on commit 86a1fa8

Please sign in to comment.