|
|
@@ -1737,6 +1737,253 @@ class TableTest extends TestCase
|
|
|
$this->assertSame($entity, $table->save($entity));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that save works with replace saveStrategy and are not deleted once they are not null
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveReplaceSaveStrategy()
|
|
|
+ {
|
|
|
+ $authors = new Table(
|
|
|
+ [
|
|
|
+ 'table' => 'authors',
|
|
|
+ 'alias' => 'Authors',
|
|
|
+ 'connection' => $this->connection,
|
|
|
+ 'entityClass' => 'Cake\ORM\Entity',
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $authors->hasMany('Articles', ['saveStrategy' => 'replace']);
|
|
|
+
|
|
|
+ $entity = $authors->newEntity([
|
|
|
+ 'name' => 'mylux',
|
|
|
+ 'articles' => [
|
|
|
+ ['title' => 'One Random Post', 'body' => 'The cake is not a lie'],
|
|
|
+ ['title' => 'Another Random Post', 'body' => 'The cake is nice'],
|
|
|
+ ['title' => 'One more random post', 'body' => 'The cake is forever']
|
|
|
+ ]
|
|
|
+ ], ['associated' => ['Articles']]);
|
|
|
+
|
|
|
+ $entity = $authors->save($entity, ['associated' => ['Articles']]);
|
|
|
+ $sizeArticles = count($entity->articles);
|
|
|
+ $this->assertEquals($sizeArticles, $authors->Articles->find('all')->where(['author_id' => $entity['id']])->count());
|
|
|
+
|
|
|
+ $articleId = $entity->articles[0]->id;
|
|
|
+ unset($entity->articles[0]);
|
|
|
+ $entity->dirty('articles', true);
|
|
|
+
|
|
|
+ $authors->save($entity, ['associated' => ['Articles']]);
|
|
|
+
|
|
|
+ $this->assertEquals($sizeArticles - 1, $authors->Articles->find('all')->where(['author_id' => $entity['id']])->count());
|
|
|
+ $this->assertTrue($authors->Articles->exists(['id' => $articleId]));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that save works with append saveStrategy not deleting or setting null anything
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveAppendSaveStrategy()
|
|
|
+ {
|
|
|
+ $authors = new Table(
|
|
|
+ [
|
|
|
+ 'table' => 'authors',
|
|
|
+ 'alias' => 'Authors',
|
|
|
+ 'connection' => $this->connection,
|
|
|
+ 'entityClass' => 'Cake\ORM\Entity',
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $authors->hasMany('Articles', ['saveStrategy' => 'append']);
|
|
|
+
|
|
|
+ $entity = $authors->newEntity([
|
|
|
+ 'name' => 'mylux',
|
|
|
+ 'articles' => [
|
|
|
+ ['title' => 'One Random Post', 'body' => 'The cake is not a lie'],
|
|
|
+ ['title' => 'Another Random Post', 'body' => 'The cake is nice'],
|
|
|
+ ['title' => 'One more random post', 'body' => 'The cake is forever']
|
|
|
+ ]
|
|
|
+ ], ['associated' => ['Articles']]);
|
|
|
+
|
|
|
+ $entity = $authors->save($entity, ['associated' => ['Articles']]);
|
|
|
+ $sizeArticles = count($entity->articles);
|
|
|
+
|
|
|
+ $this->assertEquals($sizeArticles, $authors->Articles->find('all')->where(['author_id' => $entity['id']])->count());
|
|
|
+
|
|
|
+ $articleId = $entity->articles[0]->id;
|
|
|
+ unset($entity->articles[0]);
|
|
|
+ $entity->dirty('articles', true);
|
|
|
+
|
|
|
+ $authors->save($entity, ['associated' => ['Articles']]);
|
|
|
+
|
|
|
+ $this->assertEquals($sizeArticles, $authors->Articles->find('all')->where(['author_id' => $entity['id']])->count());
|
|
|
+ $this->assertTrue($authors->Articles->exists(['id' => $articleId]));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that save has append as the default save strategy
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveDefaultSaveStrategy()
|
|
|
+ {
|
|
|
+ $authors = new Table(
|
|
|
+ [
|
|
|
+ 'table' => 'authors',
|
|
|
+ 'alias' => 'Authors',
|
|
|
+ 'connection' => $this->connection,
|
|
|
+ 'entityClass' => 'Cake\ORM\Entity',
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ $authors->hasMany('Articles', ['saveStrategy' => 'append']);
|
|
|
+ $this->assertEquals('append', $authors->association('articles')->saveStrategy());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that the associated entities are unlinked and deleted when they are dependent
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveReplaceSaveStrategyDependent()
|
|
|
+ {
|
|
|
+ $authors = new Table(
|
|
|
+ [
|
|
|
+ 'table' => 'authors',
|
|
|
+ 'alias' => 'Authors',
|
|
|
+ 'connection' => $this->connection,
|
|
|
+ 'entityClass' => 'Cake\ORM\Entity',
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $authors->hasMany('Articles', ['saveStrategy' => 'replace', 'dependent' => true]);
|
|
|
+
|
|
|
+ $entity = $authors->newEntity([
|
|
|
+ 'name' => 'mylux',
|
|
|
+ 'articles' => [
|
|
|
+ ['title' => 'One Random Post', 'body' => 'The cake is not a lie'],
|
|
|
+ ['title' => 'Another Random Post', 'body' => 'The cake is nice'],
|
|
|
+ ['title' => 'One more random post', 'body' => 'The cake is forever']
|
|
|
+ ]
|
|
|
+ ], ['associated' => ['Articles']]);
|
|
|
+
|
|
|
+ $entity = $authors->save($entity, ['associated' => ['Articles']]);
|
|
|
+ $sizeArticles = count($entity->articles);
|
|
|
+ $this->assertEquals($sizeArticles, $authors->Articles->find('all')->where(['author_id' => $entity['id']])->count());
|
|
|
+
|
|
|
+ $articleId = $entity->articles[0]->id;
|
|
|
+ unset($entity->articles[0]);
|
|
|
+ $entity->dirty('articles', true);
|
|
|
+
|
|
|
+ $authors->save($entity, ['associated' => ['Articles']]);
|
|
|
+
|
|
|
+ $this->assertEquals($sizeArticles - 1, $authors->Articles->find('all')->where(['author_id' => $entity['id']])->count());
|
|
|
+ $this->assertFalse($authors->Articles->exists(['id' => $articleId]));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that the associated entities are unlinked and deleted when they have a not nullable foreign key
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveReplaceSaveStrategyNotNullable()
|
|
|
+ {
|
|
|
+ $articles = new Table(
|
|
|
+ [
|
|
|
+ 'table' => 'articles',
|
|
|
+ 'alias' => 'Articles',
|
|
|
+ 'connection' => $this->connection,
|
|
|
+ 'entityClass' => 'Cake\ORM\Entity',
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $articles->hasMany('Comments', ['saveStrategy' => 'replace']);
|
|
|
+
|
|
|
+ $article = $articles->newEntity([
|
|
|
+ 'title' => 'Bakeries are sky rocketing',
|
|
|
+ 'body' => 'All because of cake',
|
|
|
+ 'comments' => [
|
|
|
+ [
|
|
|
+ 'user_id' => 1,
|
|
|
+ 'comment' => 'That is true!'
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'user_id' => 2,
|
|
|
+ 'comment' => 'Of course'
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ ], ['associated' => ['Comments']]);
|
|
|
+
|
|
|
+ $article = $articles->save($article, ['associated' => ['Comments']]);
|
|
|
+ $commentId = $article->comments[0]->id;
|
|
|
+ $sizeComments = count($article->comments);
|
|
|
+
|
|
|
+ $this->assertEquals($sizeComments, $articles->Comments->find('all')->where(['article_id' => $article->id])->count());
|
|
|
+ $this->assertTrue($articles->Comments->exists(['id' => $commentId]));
|
|
|
+
|
|
|
+ unset($article->comments[0]);
|
|
|
+ $article->dirty('comments', true);
|
|
|
+ $article = $articles->save($article, ['associated' => ['Comments']]);
|
|
|
+
|
|
|
+ $this->assertEquals($sizeComments - 1, $articles->Comments->find('all')->where(['article_id' => $article->id])->count());
|
|
|
+ $this->assertFalse($articles->Comments->exists(['id' => $commentId]));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that the associated entities are unlinked and deleted when they have a not nullable foreign key
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSaveReplaceSaveStrategyAdding()
|
|
|
+ {
|
|
|
+ $articles = new Table(
|
|
|
+ [
|
|
|
+ 'table' => 'articles',
|
|
|
+ 'alias' => 'Articles',
|
|
|
+ 'connection' => $this->connection,
|
|
|
+ 'entityClass' => 'Cake\ORM\Entity',
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ $articles->hasMany('Comments', ['saveStrategy' => 'replace']);
|
|
|
+
|
|
|
+ $article = $articles->newEntity([
|
|
|
+ 'title' => 'Bakeries are sky rocketing',
|
|
|
+ 'body' => 'All because of cake',
|
|
|
+ 'comments' => [
|
|
|
+ [
|
|
|
+ 'user_id' => 1,
|
|
|
+ 'comment' => 'That is true!'
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'user_id' => 2,
|
|
|
+ 'comment' => 'Of course'
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ ], ['associated' => ['Comments']]);
|
|
|
+
|
|
|
+ $article = $articles->save($article, ['associated' => ['Comments']]);
|
|
|
+ $commentId = $article->comments[0]->id;
|
|
|
+ $sizeComments = count($article->comments);
|
|
|
+ $articleId = $article->id;
|
|
|
+
|
|
|
+ $this->assertEquals($sizeComments, $articles->Comments->find('all')->where(['article_id' => $article->id])->count());
|
|
|
+ $this->assertTrue($articles->Comments->exists(['id' => $commentId]));
|
|
|
+
|
|
|
+ unset($article->comments[0]);
|
|
|
+ $article->comments[] = $articles->Comments->newEntity([
|
|
|
+ 'user_id' => 1,
|
|
|
+ 'comment' => 'new comment'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $article->dirty('comments', true);
|
|
|
+ $article = $articles->save($article, ['associated' => ['Comments']]);
|
|
|
+
|
|
|
+ $this->assertEquals($sizeComments, $articles->Comments->find('all')->where(['article_id' => $article->id])->count());
|
|
|
+ $this->assertFalse($articles->Comments->exists(['id' => $commentId]));
|
|
|
+ $this->assertTrue($articles->Comments->exists(['comment' => 'new comment', 'article_id' => $articleId]));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Test that saving a new entity with a Primary Key set does not call exists when checkExisting is false.
|
|
|
*
|