Skip to content

Commit

Permalink
Commit deferred sort orders, bug fix in join logic
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Feb 21, 2024
1 parent 2ef7f8f commit 28539e6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/Database/Concerns/HasReplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

/**
* HasReplication for a model
*
* @package october\database
* @author Alexey Bobkov, Samuel Georges
*/
trait HasReplication
{
Expand Down
11 changes: 10 additions & 1 deletion src/Database/Models/DeferredBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function beforeCreate()
* getPivotDataForBind strips attributes beginning with an underscore, allowing
* meta data to be stored using the column alongside the data.
*/
public function getPivotDataForBind(): array
public function getPivotDataForBind($model, $relationName): array
{
$data = [];

Expand All @@ -70,6 +70,15 @@ public function getPivotDataForBind(): array

$data[$key] = $value;
}

if (
$model->isClassInstanceOf(\October\Contracts\Database\SortableRelationInterface::class) &&
$model->isSortableRelation($relationName)
) {
$sortColumn = $model->getRelationSortOrderColumn($relationName);
$data[$sortColumn] = $this->sort_order;
}

return $data;
}

Expand Down
12 changes: 4 additions & 8 deletions src/Database/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,10 @@ protected function performLeftJoin($query = null)
{
$query = $query ?: $this->query;

$query->leftJoin(
$this->table,
$this->getQualifiedRelatedKeyName(),
'=',
$this->getQualifiedRelatedPivotKeyName()
);
$query->leftJoin($this->table, function($join) {
$join->on($this->getQualifiedRelatedKeyName(), '=', $this->getQualifiedRelatedPivotKeyName());
$join->where($this->getQualifiedForeignPivotKeyName(), $this->parent->getKey());
});

return $this;
}
Expand All @@ -489,8 +487,6 @@ protected function performSortableColumnJoin($query = null, $sessionKey = null)

$orderDefinitions = $query->getQuery()->orders;

traceLog($orderDefinitions);

if (!is_array($orderDefinitions)) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Database/Traits/DeferredBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use October\Rain\Database\Models\DeferredBinding as DeferredBindingModel;

/**
* DeferredBinding trait
* DeferredBinding trait is implemented by all models
*
* @package october\database
* @author Alexey Bobkov, Samuel Georges
Expand Down Expand Up @@ -196,7 +196,8 @@ protected function commitDeferredOfType($sessionKey, $include = null, $exclude =
$relationObj = $this->$relationName();
if ($binding->is_bind) {
if (in_array($relationType, ['belongsToMany', 'morphToMany', 'morphedByMany'])) {
$relationObj->add($slaveModel, null, $binding->getPivotDataForBind());
$pivotData = $binding->getPivotDataForBind($this, $relationName);
$relationObj->add($slaveModel, null, $pivotData);
}
else {
$relationObj->add($slaveModel);
Expand Down
9 changes: 6 additions & 3 deletions src/Database/Traits/SortableRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ public function initializeSortableRelation()
return;
}

$relation = $this->$relationName();

// Order already set in pivot data (assuming singular)
$column = $this->getRelationSortOrderColumn($relationName);
if (is_array($data) && array_key_exists($column, $data)) {
return;
}

// Calculate a new order
$relation = $this->$relationName();
$order = $relation->max($relation->qualifyPivotColumn($column));

foreach ((array) $attached as $id) {
$relation->updateExistingPivot($id, [$column => ++$order]);
}
Expand Down

0 comments on commit 28539e6

Please sign in to comment.