| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models\Relations; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use FluentCart\Framework\Database\Orm\Builder; |
| 9 |
use FluentCart\Framework\Database\Orm\Collection; |
| 10 |
use FluentCart\Framework\Database\Orm\Model; |
| 11 |
use FluentCart\Framework\Database\Orm\Relations\Relation; |
| 12 |
|
| 13 |
class BundleChildrenRelation extends Relation |
| 14 |
{ |
| 15 |
/** |
| 16 |
* The JSON column name |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $jsonColumn; |
| 21 |
|
| 22 |
/** |
| 23 |
* The JSON key that contains child IDs |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $jsonKey; |
| 28 |
|
| 29 |
/** |
| 30 |
* Create a new BundleChildrenRelation instance. |
| 31 |
* |
| 32 |
* @param Builder $query |
| 33 |
* @param Model $parent |
| 34 |
* @param string $jsonColumn |
| 35 |
* @param string $jsonKey |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function __construct(Builder $query, Model $parent, $jsonColumn = 'other_info', $jsonKey = 'bundle_child_ids') |
| 39 |
{ |
| 40 |
$this->jsonColumn = $jsonColumn; |
| 41 |
$this->jsonKey = $jsonKey; |
| 42 |
|
| 43 |
parent::__construct($query, $parent); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Set the base constraints on the relation query. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function addConstraints() |
| 52 |
{ |
| 53 |
if (static::$constraints) { |
| 54 |
$childIds = $this->getChildIds(); |
| 55 |
|
| 56 |
if (empty($childIds)) { |
| 57 |
// No child IDs, return empty result |
| 58 |
$this->query->whereRaw('1 = 0'); |
| 59 |
} else { |
| 60 |
$this->query->whereIn($this->related->getKeyName(), $childIds); |
| 61 |
|
| 62 |
// Preserve the order of IDs |
| 63 |
$this->query->orderByRaw( |
| 64 |
'FIELD(' . $this->related->getKeyName() . ', ' . implode(',', $childIds) . ')' |
| 65 |
); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Set the constraints for an eager load of the relation. |
| 72 |
* |
| 73 |
* @param array $models |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function addEagerConstraints(array $models) |
| 77 |
{ |
| 78 |
$allChildIds = (new Collection($models))->flatMap(function ($model) { |
| 79 |
return $this->getChildIdsFromModel($model); |
| 80 |
})->unique()->values()->all(); |
| 81 |
|
| 82 |
if (empty($allChildIds)) { |
| 83 |
$this->query->whereRaw('1 = 0'); |
| 84 |
} else { |
| 85 |
$this->query->whereIn($this->related->getKeyName(), $allChildIds); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Initialize the relation on a set of models. |
| 91 |
* |
| 92 |
* @param array $models |
| 93 |
* @param string $relation |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function initRelation(array $models, $relation) |
| 97 |
{ |
| 98 |
foreach ($models as $model) { |
| 99 |
$model->setRelation($relation, $this->related->newCollection()); |
| 100 |
} |
| 101 |
|
| 102 |
return $models; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Match the eagerly loaded results to their parents. |
| 107 |
* |
| 108 |
* @param array $models |
| 109 |
* @param Collection $results |
| 110 |
* @param string $relation |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function match(array $models, Collection $results, $relation) |
| 114 |
{ |
| 115 |
if ($results->isEmpty()) { |
| 116 |
return $models; |
| 117 |
} |
| 118 |
|
| 119 |
// Create a dictionary of results keyed by ID |
| 120 |
$dictionary = $results->keyBy($this->related->getKeyName()); |
| 121 |
|
| 122 |
// Match results to each parent model |
| 123 |
foreach ($models as $model) { |
| 124 |
$childIds = $this->getChildIdsFromModel($model); |
| 125 |
|
| 126 |
// Get the children in the correct order |
| 127 |
$children = (new Collection($childIds)) |
| 128 |
->map(function ($id) use ($dictionary) { |
| 129 |
return $dictionary->get($id); |
| 130 |
}) |
| 131 |
->filter() |
| 132 |
->values(); |
| 133 |
|
| 134 |
$model->setRelation($relation, $this->related->newCollection($children->toArray())); |
| 135 |
} |
| 136 |
|
| 137 |
return $models; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get the results of the relationship. |
| 142 |
* |
| 143 |
* @return mixed |
| 144 |
*/ |
| 145 |
public function getResults() |
| 146 |
{ |
| 147 |
if (is_null($this->parent->{$this->parent->getKeyName()})) { |
| 148 |
return $this->related->newCollection(); |
| 149 |
} |
| 150 |
|
| 151 |
return $this->get(); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get the child IDs from the parent model. |
| 156 |
* |
| 157 |
* @return array |
| 158 |
*/ |
| 159 |
protected function getChildIds() |
| 160 |
{ |
| 161 |
return $this->getChildIdsFromModel($this->parent); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get child IDs from a specific model. |
| 166 |
* |
| 167 |
* @param Model $model |
| 168 |
* @return array |
| 169 |
*/ |
| 170 |
protected function getChildIdsFromModel(Model $model) |
| 171 |
{ |
| 172 |
$jsonData = $model->{$this->jsonColumn}; |
| 173 |
|
| 174 |
if (is_string($jsonData)) { |
| 175 |
$jsonData = json_decode($jsonData, true); |
| 176 |
} |
| 177 |
|
| 178 |
$childIds = $jsonData[$this->jsonKey] ?? []; |
| 179 |
|
| 180 |
// Ensure we have an array of integers |
| 181 |
return array_filter(array_map('intval', (array) $childIds)); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Add a basic where clause to the query. |
| 186 |
* |
| 187 |
* @param string $column |
| 188 |
* @param mixed $operator |
| 189 |
* @param mixed $value |
| 190 |
* @param string $boolean |
| 191 |
* @return $this |
| 192 |
*/ |
| 193 |
public function where($column, $operator = null, $value = null, $boolean = 'and') |
| 194 |
{ |
| 195 |
$this->query->where($column, $operator, $value, $boolean); |
| 196 |
|
| 197 |
return $this; |
| 198 |
} |
| 199 |
} |