From dc641d870e07be327c26e23d59cada88019c5c04 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 21 May 2026 09:33:19 +0200 Subject: [PATCH] Model: Accept passed properties in the constructor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes a long standing oversight which wasn't even noticed when fixing access to a private property as it accessed `$this->properties` initially. This must have been a typo and the intendend check was to avoid passing an empty array and null to `setProperties`, as the constructor was always final and it is impossible the property can be non-empty at this stage. And even if so, why on earth would a base implementation only accept custom properties if it already has some… --- src/Model.php | 2 +- tests/ModelTest.php | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Model.php b/src/Model.php index 4da680f..f95427c 100644 --- a/src/Model.php +++ b/src/Model.php @@ -16,7 +16,7 @@ abstract class Model implements \ArrayAccess, \IteratorAggregate final public function __construct(?array $properties = null) { - if ($this->hasProperties()) { + if (! empty($properties)) { $this->setProperties($properties); } diff --git a/tests/ModelTest.php b/tests/ModelTest.php index ee7c553..0b6105c 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -2,6 +2,8 @@ namespace ipl\Tests\Orm; +use ipl\Orm\Model; + class ModelTest extends \PHPUnit\Framework\TestCase { public function testInitIsCalledAfterConstruction() @@ -21,4 +23,48 @@ public function testOnReturnsQueryWithModelAndDatabaseConnectionAssociated() /** @noinspection PhpParamsInspection */ $this->assertInstanceOf(TestModel::class, $query->getModel()); } + + public function testModelsCanBeInitializedWithProperties(): void + { + $model = new class (['foo' => 'bar']) extends Model { + public function getTableName() + { + return 'test'; + } + + public function getKeyName() + { + return 'id'; + } + + public function getColumns() + { + return ['foo']; + } + }; + + $this->assertSame('bar', $model->foo); + } + + public function testModelsCanBeInitializedWithoutProperties(): void + { + $model = new class extends Model { + public function getTableName() + { + return 'test'; + } + + public function getKeyName() + { + return 'id'; + } + + public function getColumns() + { + return ['foo']; + } + }; + + $this->assertFalse(isset($model->foo)); + } }