Fixed losing entity IDs upon updates

This commit is contained in:
Marcin Kurczewski 2014-09-10 19:16:06 +02:00
parent 29b173de65
commit cb08f68469
2 changed files with 20 additions and 13 deletions

View file

@ -34,8 +34,10 @@ abstract class AbstractDao implements ICrudDao
$arrayEntity = $this->entityConverter->toArray($entity); $arrayEntity = $this->entityConverter->toArray($entity);
if ($entity->id) if ($entity->id)
{ {
$savedId = $arrayEntity['_id'];
unset($arrayEntity['_id']); unset($arrayEntity['_id']);
$this->collection->update(['_id' => new \MongoId($entity->id)], $arrayEntity, ['w' => true]); $this->collection->update(['_id' => new \MongoId($entity->id)], $arrayEntity, ['w' => true]);
$arrayEntity['_id'] = $savedId;
} }
else else
{ {

View file

@ -3,23 +3,28 @@ namespace Szurubooru\Tests\Dao;
final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
public function testSaving() public function testCreating()
{ {
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post = new \Szurubooru\Entities\Post(); $post = new \Szurubooru\Entities\Post();
$post->name = 'test2'; $post->name = 'test';
$savedPost = $postDao->save($post);
$this->assertEquals('test', $post->name);
$this->assertNotNull($savedPost->id);
}
$postDao->save($post); public function testUpdating()
$post->name .= '3'; {
$postDao->save($post); $postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
$post = new \Szurubooru\Entities\Post();
$otherPost = new \Szurubooru\Entities\Post(); $post->name = 'test';
$otherPost->name = 'yo'; $post = $postDao->save($post);
$postDao->save($otherPost); $id = $post->id;
$post->name .= '2';
$this->assertEquals('test23', $post->name); $post = $postDao->save($post);
$this->assertEquals('yo', $otherPost->name); $this->assertEquals('test2', $post->name);
$this->assertEquals($id, $post->id);
} }
public function testGettingAll() public function testGettingAll()