Permalink
Browse files

[ORM] Use primary key value given as an attribute instead of trying t…

…o use sequence.
  • Loading branch information...
bbankowski committed Dec 20, 2018
1 parent a033f1d commit 9f645b8c870a0a23e683f860078ea99102745766
Showing with 31 additions and 4 deletions.
  1. +1 −0 CHANGELOG.md
  2. +9 −4 src/Ouzo/Core/Model.php
  3. +1 −0 test/Application/Model/Test/Product.php
  4. +20 −0 test/src/Ouzo/Core/ModelTest.php
@@ -8,6 +8,7 @@ Enhancements:
* [ORM] Changed Model::deleteEach to use iterator instead of fetching all elements at once (issue #254).
* [ORM] Fixed fetchIterator to properly use cursor underneath.
* [ORM] Implemented upsert functionality (Model::createOrUpdate).
* [ORM] Fixed using primary key value when given as an attribute, instead of using sequence.
* [DI] Added constructor injection for arguments with types defined (issue #265).
* [DI] Implemented injection of private fields for parent class.
* [DI] Implemented injection through factory class.
@@ -226,10 +226,15 @@ private function doInsert($callback)
$query = $callback($attributes);
$lastInsertedId = QueryExecutor::prepare($this->modelDefinition->db, $query)->insert($this->modelDefinition->sequence);
if ($primaryKey && $this->modelDefinition->sequence) {
$this->$primaryKey = $lastInsertedId;
$sequence = $primaryKey && $this->$primaryKey !== null ? null : $this->modelDefinition->sequence;
$lastInsertedId = QueryExecutor::prepare($this->modelDefinition->db, $query)->insert($sequence);
if ($primaryKey) {
if ($sequence) {
$this->$primaryKey = $lastInsertedId;
} else {
$lastInsertedId = $this->$primaryKey;
}
}
$this->callAfterSaveCallbacks();
@@ -8,6 +8,7 @@
use Ouzo\Model;
/**
* @property int id
* @property string description
* @property string name
* @property Category category
@@ -64,6 +64,26 @@ public function shouldPersistModel()
$this->assertEquals('Sport', $actual->name);
}
/**
* @test
*/
public function shouldPersistModelWithSpecifiedPrimaryKeyValue()
{
//given
$product = new Product();
$product->id = 123;
$product->name = 'Sport';
//when
$id = $product->insert();
//then
$this->assertEquals(123, $id);
$actual = Product::findById($id);
$this->assertEquals('Sport', $actual->name);
}
/**
* @test
*/

0 comments on commit 9f645b8

Please sign in to comment.