From 3dbb2b06be7b7da1a484fef93f3daed48eae5423 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 25 Oct 2014 18:26:22 +0200 Subject: [PATCH] Fixed relations being wiped out when saving The code worked like this: 1. Delete old relations 2. Get related entities 3. Save old relations If the entity hasn't retrieved relations until now, autoloader tried to get related entities /after/ they were deleted. --- src/Dao/PostDao.php | 5 ++--- src/Dao/TagDao.php | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index e24e7ee2..8d780a45 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -257,9 +257,6 @@ class PostDao extends AbstractDao implements ICrudDao private function syncPostRelations(Post $post) { - $this->pdo->deleteFrom('postRelations')->where('post1id', $post->getId())->execute(); - $this->pdo->deleteFrom('postRelations')->where('post2id', $post->getId())->execute(); - $relatedPostIds = array_filter(array_unique(array_map( function ($post) { @@ -269,6 +266,8 @@ class PostDao extends AbstractDao implements ICrudDao }, $post->getRelatedPosts()))); + $this->pdo->deleteFrom('postRelations')->where('post1id', $post->getId())->execute(); + $this->pdo->deleteFrom('postRelations')->where('post2id', $post->getId())->execute(); foreach ($relatedPostIds as $postId) { $this->pdo diff --git a/src/Dao/TagDao.php b/src/Dao/TagDao.php index 2796cc58..17f9d4d8 100644 --- a/src/Dao/TagDao.php +++ b/src/Dao/TagDao.php @@ -188,11 +188,6 @@ class TagDao extends AbstractDao implements ICrudDao private function syncRelatedTagsByType(Tag $tag, array $relatedTags, $type) { - $this->pdo->deleteFrom('tagRelations') - ->where('tag1id', $tag->getId()) - ->where('type', $type) - ->execute(); - $relatedTagIds = array_filter(array_unique(array_map( function ($tag) { @@ -202,6 +197,11 @@ class TagDao extends AbstractDao implements ICrudDao }, $relatedTags))); + $this->pdo->deleteFrom('tagRelations') + ->where('tag1id', $tag->getId()) + ->where('type', $type) + ->execute(); + foreach ($relatedTagIds as $tagId) { $this->pdo