Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 9, 2024
1 parent 9690243 commit 190abc6
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/persistence.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ protected function init(): void
$this->getOwner()->hasOne(
$f . '_currency_id',
[
$this->currency_model ?? new Currency(),
'model' => $this->currency_model ?? new Currency(),
'system' => true,
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(array $defaults = [])
{
$this->setDefaults($defaults);

if (!(new \ReflectionProperty($this, 'type'))->isInitialized($this)) {
if (($this->type ?? null) === null) {
$this->type = 'string';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Reference/ContainsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function init(): void
{
parent::init();

if (!$this->ourField) {
if ($this->ourField === null) {
$this->ourField = $this->link;
}

Expand Down
10 changes: 6 additions & 4 deletions src/Reference/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function getModelTableString(Model $model): string
#[\Override]
public function getTheirFieldName(Model $theirModel = null): string
{
if ($this->theirField) {
if ($this->theirField !== null) {
return $this->theirField;
}

Expand All @@ -50,7 +50,7 @@ protected function getOurFieldValueForRefCondition(Model $ourModelOrEntity)
$this->assertOurModelOrEntity($ourModelOrEntity);

if ($ourModelOrEntity->isEntity()) {
$res = $this->ourField
$res = $this->ourField !== null
? $ourModelOrEntity->get($this->ourField)
: $ourModelOrEntity->getId();
$this->assertReferenceValueNotNull($res);
Expand Down Expand Up @@ -96,9 +96,11 @@ public function ref(Model $ourModelOrEntity, array $defaults = []): Model
*
* @param array<string, mixed> $defaults
*/
public function refLink(?Model $ourModel, array $defaults = []): Model
public function refLink(?Model $ourModelOrEntity, array $defaults = []): Model
{
$this->getOurModel($ourModel); // or should $this->assertOurModelOrEntity($ourModelOrEntity); be here? What is exactly the difference between ref and refLink?
if ($ourModelOrEntity !== null) { // TODO drop this parameter, refLink for entity is useless
$this->assertOurModelOrEntity($ourModelOrEntity);
}

$theirModelLinked = $this->createTheirModel($defaults)->addCondition(
$this->getTheirFieldName(),
Expand Down
6 changes: 3 additions & 3 deletions src/Reference/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ protected function init(): void
{
parent::init();

if (!$this->ourField) {
if ($this->ourField === null) {
$this->ourField = $this->link;
}

// for references use "integer" as a default type
if (!(new \ReflectionProperty($this, 'type'))->isInitialized($this)) {
if (($this->type ?? null) === null) {
$this->type = 'integer';
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public function ref(Model $ourModelOrEntity, array $defaults = []): Model

if ($ourModelOrEntity->isEntity()) {
$this->onHookToTheirModel($theirModel, Model::HOOK_AFTER_SAVE, function (Model $theirEntity) use ($ourModelOrEntity) {
$theirValue = $this->theirField
$theirValue = $this->theirField !== null
? $theirEntity->get($this->theirField)
: $theirEntity->getId();

Expand Down
30 changes: 15 additions & 15 deletions src/Reference/HasOneSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function addField(string $fieldName, string $theirFieldName = null, array

$ourModel = $this->getOurModel(null);

// if caption/type is not defined in $defaults -> get it directly from the linked model field $theirFieldName
// if caption/type is not defined in $defaults then infer it from their field
$refModel = $ourModel->getReference($this->link)->createTheirModel();
$refModelField = $refModel->getField($theirFieldName);
$defaults['type'] ??= $refModelField->type;
Expand Down Expand Up @@ -122,20 +122,6 @@ public function addFields(array $fields = [], array $defaults = [])
return $this;
}

/**
* Creates model that can be used for generating sub-query actions.
*
* @param array<string, mixed> $defaults
*/
public function refLink(Model $ourModel, array $defaults = []): Model
{
$theirModel = $this->createTheirModel($defaults);

$theirModel->addCondition($this->getTheirFieldName($theirModel), $this->referenceOurValue());

return $theirModel;
}

#[\Override]
public function ref(Model $ourModelOrEntity, array $defaults = []): Model
{
Expand All @@ -156,6 +142,20 @@ public function ref(Model $ourModelOrEntity, array $defaults = []): Model
return $theirModel;
}

/**
* Creates model that can be used for generating sub-query actions.
*
* @param array<string, mixed> $defaults
*/
public function refLink(Model $ourModelOrEntity, array $defaults = []): Model
{
$theirModel = $this->createTheirModel($defaults);

$theirModel->addCondition($this->getTheirFieldName($theirModel), $this->referenceOurValue());

return $theirModel;
}

/**
* Add a title of related entity as expression to our field.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/ContainsOne/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function init(): void
{
parent::init();

$this->hasOne($this->fieldName()->country_id, ['model' => [Country::class], 'type' => 'integer']);
$this->hasOne($this->fieldName()->country_id, ['model' => [Country::class]]);

$this->addField($this->fieldName()->address);
$this->addField($this->fieldName()->built_date, ['type' => 'datetime']);
Expand Down
2 changes: 1 addition & 1 deletion tests/Persistence/Sql/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($defaults = [], array $arguments = [])
}
};

if (!(new \ReflectionProperty($query, 'connection'))->isInitialized($query)) {
if (($query->connection ?? null) === null) {
$query->connection = \Closure::bind(static function () use ($query) {
$connection = new Persistence\Sql\Sqlite\Connection();
$connection->expressionClass = \Closure::bind(static fn () => $query->expressionClass, null, Query::class)();
Expand Down

0 comments on commit 190abc6

Please sign in to comment.