Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not deleted when hasMany relations saving #256

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions lib/Relation/RelationAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
42 changes: 42 additions & 0 deletions tests/CRUD.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace SpotTest;

use Datetime;

/**
* @package Spot
*/
Expand Down Expand Up @@ -554,4 +556,44 @@ public function testQueryWithDateTimeObjectValue($post)

$this->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' => '[email protected]',
'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' => '[email protected]',
'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);
}
}
5 changes: 2 additions & 3 deletions tests/Entity/Post/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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 [
Expand Down
2 changes: 1 addition & 1 deletion tests/Scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}