From 11742b763191a82e2c2b8ac3daf5c7d12b5b29fc Mon Sep 17 00:00:00 2001 From: kitchenu Date: Wed, 13 Dec 2017 16:08:18 +0900 Subject: [PATCH 1/2] Fix not deleted when hasMany relations saving Fix not deleted when hasMany relations saving --- lib/Relation/HasMany.php | 4 ++-- tests/Entity/Post/Comment.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Relation/HasMany.php b/lib/Relation/HasMany.php index 0366b15..8895411 100644 --- a/lib/Relation/HasMany.php +++ b/lib/Relation/HasMany.php @@ -110,7 +110,7 @@ public function save(EntityInterface $entity, $relationName, $options = []) $related->set($this->foreignKey(), $entity->primaryKey()); $lastResult = $relatedMapper->save($related, $options); } - $relatedIds[] = $related->id; + $relatedIds[] = $related->primaryKey(); } foreach ($oldEntities as $oldRelatedEntity) { @@ -122,7 +122,7 @@ public function save(EntityInterface $entity, $relationName, $options = []) if (count($deletedIds) || $relatedEntities === false) { $conditions = [$this->foreignKey() => $entity->primaryKey()]; if (count($deletedIds)) { - $conditions[$this->localKey().' :in'] = $deletedIds; + $conditions[$relatedMapper->primaryKeyField().' :in'] = $deletedIds; } if ($relatedMapper->entityManager()->fields()[$this->foreignKey()]['notnull']) { $relatedMapper->delete($conditions); diff --git a/tests/Entity/Post/Comment.php b/tests/Entity/Post/Comment.php index fef6923..5f33243 100644 --- a/tests/Entity/Post/Comment.php +++ b/tests/Entity/Post/Comment.php @@ -19,7 +19,7 @@ class Comment extends Entity public static function fields() { return [ - 'id' => ['type' => 'integer', 'primary' => true, 'autoincrement' => true], + 'comment_id' => ['type' => 'integer', 'primary' => true, 'autoincrement' => true], 'post_id' => ['type' => 'integer', 'index' => true, 'required' => true], 'name' => ['type' => 'string', 'required' => true], 'email' => ['type' => 'string', 'required' => true], From 9942508cf82a16ac0714a514fea3a292b21e517a Mon Sep 17 00:00:00 2001 From: kitchenu Date: Mon, 15 Jan 2018 17:20:09 +0900 Subject: [PATCH 2/2] Add relations result and query reset function --- lib/Relation/RelationAbstract.php | 14 +++++++++++ tests/CRUD.php | 42 +++++++++++++++++++++++++++++++ tests/Entity/Post/Comment.php | 3 +-- tests/Scopes.php | 2 +- 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/Relation/RelationAbstract.php b/lib/Relation/RelationAbstract.php index 2f9faf5..b15c6c5 100644 --- a/lib/Relation/RelationAbstract.php +++ b/lib/Relation/RelationAbstract.php @@ -166,6 +166,20 @@ public function execute() return $this->result; } + /** + * Reset result and query + * + * @return $this + */ + public function reset() + { + $this->result = null; + $this->query = null; + $this->queryQueue = []; + + return $this; + } + /** * Save related entities * diff --git a/tests/CRUD.php b/tests/CRUD.php index 89a41f5..ea8fd7b 100644 --- a/tests/CRUD.php +++ b/tests/CRUD.php @@ -1,6 +1,8 @@ assertTrue(count($results) > 0); } + + /** + * @group save-relations + */ + public function testResetRelationResult() + { + $mapper = test_spot_mapper('SpotTest\Entity\Post'); + $commentMapper = test_spot_mapper('SpotTest\Entity\Post\Comment'); + $comments = [ + new \SpotTest\Entity\Post\Comment([ + 'name' => 'John Doe', + 'email' => 'test@example.com', + 'body' => '#1: Lorem ipsum is dolor.', + 'date_created' => new DateTime('yesterday') + ]) + ]; + for ($i = 2; $i < 4; $i++) { + $comments[] = new \SpotTest\Entity\Post\Comment([ + 'name' => 'John Doe', + 'email' => 'test@example.com', + 'body' => '#'.$i.': Lorem ipsum is dolor.', + 'date_created' => new DateTime('today') + ]); + } + $post = $mapper->build([ + 'title' => 'Test', + 'body' => 'Test description', + 'author_id' => 1 + ]); + $post->relation('comments', new \Spot\Entity\Collection($comments)); + $mapper->save($post, ['relations' => true]); + + $post = $mapper->get($post->id); + + $comments = $post->comments->yesterday()->execute(); + $this->assertEquals($comments->count(), 1); + + $comments = $post->comments->reset()->execute(); + $this->assertEquals($comments->count(), 3); + } } diff --git a/tests/Entity/Post/Comment.php b/tests/Entity/Post/Comment.php index 5f33243..7d8ddc0 100644 --- a/tests/Entity/Post/Comment.php +++ b/tests/Entity/Post/Comment.php @@ -32,12 +32,11 @@ public static function scopes() { return [ 'yesterday' => function (Query $query) { - return $query->where(['date_created :gt' => new DateTime('yesterday'), 'date_created :lt' => new DateTime('today')]); + return $query->where(['date_created :gte' => new DateTime('yesterday'), 'date_created :lt' => new DateTime('today')]); } ]; } - public static function relations(MapperInterface $mapper, EntityInterface $entity) { return [ diff --git a/tests/Scopes.php b/tests/Scopes.php index 4406ee7..e2c05a6 100644 --- a/tests/Scopes.php +++ b/tests/Scopes.php @@ -66,6 +66,6 @@ public function testRelationScopes() ]); $query = $mapper->get(1)->comments->yesterday()->query(); $sql = str_replace(['`', '"'], '', $query->toSql()); - $this->assertEquals("SELECT * FROM test_post_comments WHERE (test_post_comments.post_id = ?) AND (test_post_comments.date_created > ? AND test_post_comments.date_created < ?) ORDER BY test_post_comments.date_created ASC", $sql); + $this->assertEquals("SELECT * FROM test_post_comments WHERE (test_post_comments.post_id = ?) AND (test_post_comments.date_created >= ? AND test_post_comments.date_created < ?) ORDER BY test_post_comments.date_created ASC", $sql); } }