Browse Source

Marking entities on save and fixing more tests

Jose Lorenzo Rodriguez 12 years ago
parent
commit
cd2c2720b0

+ 5 - 0
src/ORM/Association/BelongsToMany.php

@@ -514,6 +514,10 @@ class BelongsToMany extends Association {
 		$targetPrimaryKey = (array)$target->primaryKey();
 		$sourcePrimaryKey = (array)$source->primaryKey();
 		$jointProperty = $this->_junctionProperty;
+		$junctionInfo = [
+			'alias' => $junction->alias(),
+			'className' => get_class($junction)
+		];
 
 		foreach ($targetEntities as $k => $e) {
 			$joint = $e->get($jointProperty);
@@ -535,6 +539,7 @@ class BelongsToMany extends Association {
 
 			$e->set($jointProperty, $joint);
 			$e->dirty($jointProperty, false);
+			$joint->source($junctionInfo);
 		}
 
 		return true;

+ 4 - 0
src/ORM/Table.php

@@ -1170,6 +1170,10 @@ class Table implements RepositoryInterface, EventListener {
 				$event = new Event('Model.afterSave', $this, compact('entity', 'options'));
 				$this->getEventManager()->dispatch($event);
 				$entity->isNew(false);
+				$entity->source([
+					'alias' => $this->alias(),
+					'className' => get_class($this)
+				]);
 				$success = true;
 			}
 		}

+ 14 - 10
tests/TestCase/ORM/Association/BelongsToManyTest.php

@@ -1010,19 +1010,23 @@ class BelongsToManyTest extends TestCase {
 
 		$joint->expects($this->at(0))
 			->method('save')
-			->with(
-				new Entity(['article_id' => 1, 'tag_id' => 2], ['markNew' => true]),
-				$saveOptions
-			)
-			->will($this->returnValue($entity));
+			->will($this->returnCallback(function($e, $opts) use ($entity) {
+				$expected = ['article_id' => 1, 'tag_id' => 2];
+				$this->assertEquals($expected, $e->toArray());
+				$this->assertEquals(['foo' => 'bar'], $opts);
+				$this->assertTrue($e->isNew());
+				return $entity;
+			}));
 
 		$joint->expects($this->at(1))
 			->method('save')
-			->with(
-				new Entity(['article_id' => 1, 'tag_id' => 3], ['markNew' => true]),
-				$saveOptions
-			)
-			->will($this->returnValue($entity));
+			->will($this->returnCallback(function($e, $opts) use ($entity) {
+				$expected = ['article_id' => 1, 'tag_id' => 3];
+				$this->assertEquals($expected, $e->toArray());
+				$this->assertEquals(['foo' => 'bar'], $opts);
+				$this->assertTrue($e->isNew());
+				return $entity;
+			}));
 
 		$this->assertTrue($assoc->link($entity, $tags, $saveOptions));
 		$this->assertSame($entity->test, $tags);

+ 9 - 5
tests/TestCase/ORM/TableTest.php

@@ -2692,9 +2692,7 @@ class TableTest extends \Cake\TestSuite\TestCase {
 		$article = $table->find('all')->where(['id' => 1])->contain(['tags'])->first();
 		$tags = $article->tags;
 		$this->assertNotEmpty($tags);
-		$tags[] = new \TestApp\Model\Entity\Tag([
-			'name' => 'Something New'
-		]);
+		$tags[] = new \TestApp\Model\Entity\Tag(['name' => 'Something New']);
 		$article->tags = $tags;
 		$this->assertSame($article, $table->save($article));
 		$tags = $article->tags;
@@ -2817,6 +2815,12 @@ class TableTest extends \Cake\TestSuite\TestCase {
 		$table = TableRegistry::get('articles');
 		$table->belongsToMany('tags');
 		$tagsTable = TableRegistry::get('tags');
+		$source = [
+			'source' => [
+				'alias' => 'tags',
+				'className' => 'TestApp\Model\Table\TagsTable'
+			]
+		];
 		$options = ['markNew' => false];
 
 		$article = new \Cake\ORM\Entity([
@@ -2825,10 +2829,10 @@ class TableTest extends \Cake\TestSuite\TestCase {
 
 		$newTag = new \TestApp\Model\Entity\Tag([
 			'name' => 'Foo'
-		]);
+		], $source);
 		$tags[] = new \TestApp\Model\Entity\Tag([
 			'id' => 3
-		], $options);
+		], $options + $source);
 		$tags[] = $newTag;
 
 		$tagsTable->save($newTag);

+ 1 - 1
tests/test_app/TestApp/Model/Table/ArticlesTable.php

@@ -22,7 +22,7 @@ class ArticlesTable extends Table {
 	public function initialize(array $config) {
 		$this->belongsTo('authors');
 		$this->belongsToMany('tags');
-		$this->hasMany('articlesTags');
+		$this->hasMany('ArticlesTags');
 	}
 
 }

+ 1 - 1
tests/test_app/TestApp/Model/Table/TagsTable.php

@@ -22,7 +22,7 @@ class TagsTable extends Table {
 	public function initialize(array $config) {
 		$this->belongsTo('authors');
 		$this->belongsToMany('articles');
-		$this->hasMany('articlesTags', ['propertyName' => 'extraInfo']);
+		$this->hasMany('ArticlesTags', ['propertyName' => 'extraInfo']);
 	}
 
 }