Fixed losing entity IDs upon updates
This commit is contained in:
parent
29b173de65
commit
cb08f68469
2 changed files with 20 additions and 13 deletions
|
@ -34,8 +34,10 @@ abstract class AbstractDao implements ICrudDao
|
|||
$arrayEntity = $this->entityConverter->toArray($entity);
|
||||
if ($entity->id)
|
||||
{
|
||||
unset ($arrayEntity['_id']);
|
||||
$savedId = $arrayEntity['_id'];
|
||||
unset($arrayEntity['_id']);
|
||||
$this->collection->update(['_id' => new \MongoId($entity->id)], $arrayEntity, ['w' => true]);
|
||||
$arrayEntity['_id'] = $savedId;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -3,23 +3,28 @@ namespace Szurubooru\Tests\Dao;
|
|||
|
||||
final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
||||
{
|
||||
public function testSaving()
|
||||
public function testCreating()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
||||
|
||||
$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);
|
||||
$post->name .= '3';
|
||||
$postDao->save($post);
|
||||
|
||||
$otherPost = new \Szurubooru\Entities\Post();
|
||||
$otherPost->name = 'yo';
|
||||
$postDao->save($otherPost);
|
||||
|
||||
$this->assertEquals('test23', $post->name);
|
||||
$this->assertEquals('yo', $otherPost->name);
|
||||
public function testUpdating()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
||||
$post = new \Szurubooru\Entities\Post();
|
||||
$post->name = 'test';
|
||||
$post = $postDao->save($post);
|
||||
$id = $post->id;
|
||||
$post->name .= '2';
|
||||
$post = $postDao->save($post);
|
||||
$this->assertEquals('test2', $post->name);
|
||||
$this->assertEquals($id, $post->id);
|
||||
}
|
||||
|
||||
public function testGettingAll()
|
||||
|
|
Loading…
Reference in a new issue