Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions src/Database/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?php namespace Winter\Storm\Database\Concerns;

use Winter\Storm\Support\Str;
use InvalidArgumentException;
use Winter\Storm\Database\Model;
use Winter\Storm\Database\Relations\AttachMany;
use Winter\Storm\Database\Relations\AttachOne;
use Winter\Storm\Database\Relations\BelongsTo;
use Winter\Storm\Database\Relations\BelongsToMany;
use Winter\Storm\Database\Relations\HasMany;
use Winter\Storm\Database\Relations\HasManyThrough;
use Winter\Storm\Database\Relations\HasOne;
use Winter\Storm\Database\Relations\HasOneThrough;
use Winter\Storm\Database\Relations\MorphMany;
use Winter\Storm\Database\Relations\MorphToMany;
use Winter\Storm\Database\Relations\MorphTo;
use Winter\Storm\Database\Relations\MorphOne;
use Winter\Storm\Database\Relations\AttachMany;
use Winter\Storm\Database\Relations\AttachOne;
use Winter\Storm\Database\Relations\HasManyThrough;
use Winter\Storm\Database\Relations\HasOneThrough;
use InvalidArgumentException;
use Winter\Storm\Database\Relations\MorphTo;
use Winter\Storm\Database\Relations\MorphToMany;
use Winter\Storm\Support\Str;

trait HasRelationships
{
Expand Down Expand Up @@ -131,7 +132,7 @@ trait HasRelationships
'attachOne',
'attachMany',
'hasOneThrough',
'hasManyThrough'
'hasManyThrough',
];

//
Expand All @@ -141,19 +142,17 @@ trait HasRelationships
/**
* Checks if model has a relationship by supplied name.
* @param string $name Relation name
* @return bool
*/
public function hasRelation($name)
public function hasRelation($name): bool
{
return $this->getRelationDefinition($name) !== null;
}

/**
* Returns relationship details from a supplied name.
* @param string $name Relation name
* @return array|null
*/
public function getRelationDefinition($name)
public function getRelationDefinition($name): ?array
{
if (($type = $this->getRelationType($name)) !== null) {
return (array) $this->getRelationTypeDefinition($type, $name) + $this->getRelationDefaults($type);
Expand Down Expand Up @@ -195,9 +194,8 @@ public function getRelationTypeDefinition($type, $name)

/**
* Returns relationship details for all relations defined on this model.
* @return array
*/
public function getRelationDefinitions()
public function getRelationDefinitions(): array
{
$result = [];

Expand All @@ -219,10 +217,8 @@ public function getRelationDefinitions()

/**
* Returns a relationship type based on a supplied name.
* @param string $name Relation name
* @return string|null
*/
public function getRelationType($name)
public function getRelationType(string $name): ?string
{
foreach (static::$relationTypes as $type) {
if ($this->getRelationTypeDefinition($type, $name) !== null) {
Expand All @@ -234,11 +230,9 @@ public function getRelationType($name)
}

/**
* Returns a relation class object
* @param string $name Relation name
* @return \Winter\Storm\Database\Relations\Relation|null
* Returns a new instance of a related model
*/
public function makeRelation($name)
public function makeRelation(string $name): ?Model
{
$relationType = $this->getRelationType($name);
$relation = $this->getRelationDefinition($name);
Expand All @@ -248,7 +242,7 @@ public function makeRelation($name)
}

$relationClass = $relation[0];
return new $relationClass();
return $this->newRelatedInstance($relationClass);
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/Database/Models/DeferredBinding.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Winter\Storm\Database\Models;

use Exception;
use Carbon\Carbon;
use Exception;
use Winter\Storm\Database\Model;

/**
Expand Down Expand Up @@ -74,7 +74,7 @@ protected function findBindingRecord()
/**
* Cancel all deferred bindings to this model.
*/
public static function cancelDeferredActions($masterType, $sessionKey)
public static function cancelDeferredActions(string $masterType, string $sessionKey): void
{
$records = self::where('master_type', $masterType)
->where('session_key', $sessionKey)
Expand All @@ -88,7 +88,7 @@ public static function cancelDeferredActions($masterType, $sessionKey)
/**
* Delete this binding and cancel is actions
*/
public function deleteCancel()
public function deleteCancel(): void
{
$this->deleteSlaveRecord();
$this->delete();
Expand All @@ -97,7 +97,7 @@ public function deleteCancel()
/**
* Clean up orphan bindings.
*/
public static function cleanUp($days = 5)
public static function cleanUp(int $days = 5): void
{
$records = self::where('created_at', '<', Carbon::now()->subDays($days)->toDateTimeString())->get();

Expand All @@ -109,7 +109,7 @@ public static function cleanUp($days = 5)
/**
* Logic to cancel a bindings action.
*/
protected function deleteSlaveRecord()
protected function deleteSlaveRecord(): void
{
/*
* Try to delete unbound hasOne/hasMany records from the details table
Expand Down Expand Up @@ -144,8 +144,7 @@ protected function deleteSlaveRecord()
if (!$relatedObj->$foreignKey) {
$relatedObj->delete();
}
}
catch (Exception $ex) {
} catch (Exception $ex) {
// Do nothing
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/Database/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function add(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
$this->associate($model);
}
else {
} else {
$this->child->bindDeferred($this->relationName, $model, $sessionKey);
}
}
Expand All @@ -48,8 +47,7 @@ public function remove(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
$this->dissociate();
}
else {
} else {
$this->child->unbindDeferred($this->relationName, $model, $sessionKey);
}
}
Expand Down Expand Up @@ -78,8 +76,7 @@ public function setSimpleValue($value)

$this->associate($value);
$this->child->setRelation($this->relationName, $value);
}
else {
} else {
$this->child->setAttribute($this->getForeignKeyName(), $value);
$this->child->reloadRelations($this->relationName);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Relations/Concerns/AttachOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ public function add(Model $model, $sessionKey = null)
*
*/
$this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]);
}
else {
} else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Relations/Concerns/BelongsOrMorphsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ public function add(Model $model, $sessionKey = null, $pivotData = [])
if ($sessionKey === null || $sessionKey === false) {
$this->attach($model, $pivotData);
$this->parent->reloadRelations($this->relationName);
}
else {
} else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey, $pivotData);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Relations/Concerns/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public function add(Model $model, $sessionKey = null)
*
*/
$this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]);
}
else {
} else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Database/Relations/Concerns/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public function add(Model $model, $sessionKey = null)
*
*/
$this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]);
}
else {
} else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
}
}
Expand Down
33 changes: 15 additions & 18 deletions src/Database/Traits/DeferredBinding.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Winter\Storm\Database\Traits;

use Winter\Storm\Database\Collection;
use Illuminate\Database\Eloquent\Model;
use Winter\Storm\Database\Models\DeferredBinding as DeferredBindingModel;

trait DeferredBinding
Expand All @@ -12,7 +14,7 @@ trait DeferredBinding
/**
* Returns true if a relation exists and can be deferred.
*/
public function isDeferrable($relationName)
public function isDeferrable(string $relationName): bool
{
if (!$this->hasRelation($relationName)) {
return false;
Expand All @@ -27,7 +29,7 @@ public function isDeferrable($relationName)
/**
* Bind a deferred relationship to the supplied record.
*/
public function bindDeferred($relation, $record, $sessionKey, $pivotData = [])
public function bindDeferred(string $relation, Model $record, string $sessionKey, array $pivotData = []): DeferredBindingModel
{
$binding = new DeferredBindingModel;
$binding->setConnection($this->getConnectionName());
Expand All @@ -45,7 +47,7 @@ public function bindDeferred($relation, $record, $sessionKey, $pivotData = [])
/**
* Unbind a deferred relationship to the supplied record.
*/
public function unbindDeferred($relation, $record, $sessionKey)
public function unbindDeferred(string $relation, Model $record, string $sessionKey): DeferredBindingModel
{
$binding = new DeferredBindingModel;
$binding->setConnection($this->getConnectionName());
Expand All @@ -62,15 +64,15 @@ public function unbindDeferred($relation, $record, $sessionKey)
/**
* Cancel all deferred bindings to this model.
*/
public function cancelDeferred($sessionKey)
public function cancelDeferred(string $sessionKey): void
{
DeferredBindingModel::cancelDeferredActions(get_class($this), $sessionKey);
}

/**
* Commit all deferred bindings to this model.
*/
public function commitDeferred($sessionKey)
public function commitDeferred(string $sessionKey): void
{
$this->commitDeferredOfType($sessionKey);
DeferredBindingModel::flushDuplicateCache();
Expand All @@ -81,15 +83,15 @@ public function commitDeferred($sessionKey)
* It is a rare need to have to call this, since it only applies to the
* "belongs to" relationship which generally does not need deferring.
*/
protected function commitDeferredBefore($sessionKey)
protected function commitDeferredBefore(string $sessionKey): void
{
$this->commitDeferredOfType($sessionKey, 'belongsTo');
}

/**
* Internally used method to commit all deferred bindings after saving.
*/
protected function commitDeferredAfter($sessionKey)
protected function commitDeferredAfter(string $sessionKey): void
{
$this->commitDeferredOfType($sessionKey, null, 'belongsTo');
DeferredBindingModel::flushDuplicateCache();
Expand All @@ -98,7 +100,7 @@ protected function commitDeferredAfter($sessionKey)
/**
* Internal method for committing deferred relations.
*/
protected function commitDeferredOfType($sessionKey, $include = null, $exclude = null)
protected function commitDeferredOfType(string $sessionKey, string|array|null $include = null, string|array|null $exclude = null): void
{
if (!strlen($sessionKey)) {
return;
Expand All @@ -120,8 +122,7 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude =

if ($include) {
$allowedTypes = array_intersect($allowedTypes, (array) $include);
}
elseif ($exclude) {
} elseif ($exclude) {
$allowedTypes = array_diff($allowedTypes, (array) $exclude);
}

Expand All @@ -132,8 +133,7 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude =
/*
* Find the slave model
*/
$slaveClass = $binding->slave_type;
$slaveModel = new $slaveClass;
$slaveModel = $this->makeRelation($relationName) ?: new $binding->slave_type;
$slaveModel = $slaveModel->find($binding->slave_id);

if (!$slaveModel) {
Expand All @@ -152,8 +152,7 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude =
} else {
$relationObj->add($slaveModel);
}
}
else {
} else {
$relationObj->remove($slaveModel);
}

Expand All @@ -163,9 +162,8 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude =

/**
* Returns any outstanding binding records for this model.
* @return \Winter\Storm\Database\Collection
*/
protected function getDeferredBindingRecords($sessionKey)
protected function getDeferredBindingRecords(string $sessionKey): Collection
{
$binding = new DeferredBindingModel;

Expand All @@ -180,9 +178,8 @@ protected function getDeferredBindingRecords($sessionKey)

/**
* Returns all possible relation types that can be deferred.
* @return array
*/
protected function getDeferrableRelationTypes()
protected function getDeferrableRelationTypes(): array
{
return [
'hasMany',
Expand Down