diff --git a/lib/GaletteAuto/Model.php b/lib/GaletteAuto/Model.php index 3c7278b..5031eb7 100644 --- a/lib/GaletteAuto/Model.php +++ b/lib/GaletteAuto/Model.php @@ -134,6 +134,12 @@ public function store(bool $new = false): bool $insert = $this->zdb->insert(AUTO_PREFIX . self::TABLE); $insert->values($values); $this->zdb->execute($insert); + /** @phpstan-ignore-next-line */ + $this->id = (int)$this->zdb->driver->getLastGeneratedValue( + $this->zdb->isPostgres() ? + PREFIX_DB . self::TABLE . '_id_seq' + : null + ); } else { $update = $this->zdb->update(AUTO_PREFIX . self::TABLE); $update->set($values)->where( diff --git a/tests/GaletteAuto/tests/units/Model.php b/tests/GaletteAuto/tests/units/Model.php index 87e7dd8..e76cbe5 100644 --- a/tests/GaletteAuto/tests/units/Model.php +++ b/tests/GaletteAuto/tests/units/Model.php @@ -136,23 +136,46 @@ public function testCrud(): void $this->assertCount(1, $models->getList($first_brand_id)); $this->assertCount(0, $models->getList($second_brand_id)); - /*$this->assertCount(2, $brand->getList()); - $this->assertSame('2 brands', $brand->displayCount()); + $model = new \GaletteAuto\Model($this->zdb); + $data = [ + 'model' => 'A', + 'brand' => $first_brand_id, + ]; + $this->assertTrue($model->check($data)); + $this->assertTrue($model->store(true)); + $id_model = $model->id; - $brand = new \GaletteAuto\Brand($this->zdb); - $this->assertTrue($brand->load($id)); - $brand->value = 'Mercedes'; - $this->assertTrue($brand->store()); + $this->assertCount(2, $models->getList()); - $this->assertCount(2, $brand->getList()); - $this->assertSame('2 brands', $brand->displayCount()); + $data = [ + 'model' => 'A5', + 'brand' => $first_brand_id + ]; + $this->assertTrue($model->check($data)); + $this->assertTrue($model->store()); - $brand = new \GaletteAuto\Brand($this->zdb); - $this->assertTrue($brand->delete([$first_id])); - $list = $brand->getList(); - $this->assertCount(1, $list); - $last_brand = $list[0]; - $this->assertSame($id, $last_brand->id_brand);*/ + $this->assertCount(2, $models->getList()); + $this->assertCount(2, $models->getList($first_brand_id)); + $this->assertCount(0, $models->getList($second_brand_id)); + + $model = new \GaletteAuto\Model($this->zdb); + $data = [ + 'model' => 'Class S', + 'brand' => $second_brand_id, + ]; + $this->assertTrue($model->check($data)); + $this->assertTrue($model->store(true)); + + $this->assertCount(3, $models->getList()); + $this->assertCount(2, $models->getList($first_brand_id)); + $this->assertCount(1, $models->getList($second_brand_id)); + + $model = new \GaletteAuto\Model($this->zdb); + $this->assertTrue($model->delete([$id_model])); + + $this->assertCount(2, $models->getList()); + $this->assertCount(1, $models->getList($first_brand_id)); + $this->assertCount(1, $models->getList($second_brand_id)); } /**