Laravel: рекурсивно вычислить количество родителей красноречивых отношений (получить глубину отношений)
Рассмотрим диаграмму узлов ниже:
Как показано на изображении выше, я хочу создать функцию, которая может сообщить мне глубину связи с узлом верхнего уровня.
Модель экземпляра
/**
* Get the immediate parent instance of the instance.
*/
public function parent()
{
return $this->hasOne(Instance::class);
}
/**
* Get the children instances of the instance.
*/
public function children()
{
return $this->hasMany(Instance::class);
}
/**
* Get the depth of an instance.
* @return int
*/
public function getDepthAttribute()
{
// TODO
}
Таблица экземпляров
Schema::create('instances', function (Blueprint $table) {
$table->id();
$table->string('name', 32);
$table->foreignId('instance_id')->nullable();
$table->timestamps();
});
Schema::table('instances', function (Blueprint $table)
{
$table->foreign('instance_id')->references('id')->on('instances')->onUpdate('cascade')->onDelete('cascade');
});
Пример таблицы экземпляров
id name instance_id | (getDepthAttribute() should return)
-------------------------- |
1 A NULL | 0
2 B 1 | 1
3 C 2 | 2
4 D 3 | 3
5 E 3 | 3
6 F 3 | 3
Короче говоря, моя проблема такова: «Если у экземпляра есть родительский экземпляр, добавьте 1. Повторяйте, пока у родительского экземпляра не будет родительского экземпляра. Затем верните окончательное значение».
Как я могу сделать это правильно в Laravel?
2 ответа
Ниже приведено решение, которое я придумал:
DepthHelper.php
use \App\Models\Instance;
/**
* Returns the depth of an Instance
* @param $idToFind
* @return int
*/
function DepthHelper($idToFind){
return GetParentHelper($idToFind);
}
// Recursive Helper function
function GetParentHelper($id, $depth = 0) {
$model = Instance::find($id);
if ($model->instance_id != null) {
$depth++;
return GetParentHelper($model->instance_id, $depth);
} else {
return $depth;
}
}
Модель экземпляра
/**
* Get the depth of this instance from the top-level instance.
*/
public function getDepthAttribute()
{
return DepthHelper($this->id);
}
protected array $appends = ['depth'];
Мое решение не использует рекурсию, и я не знаю, как оно будет работать с большими данными. Я не думаю, что это будет проблемой, но я предлагаю вам заполнить огромную таблицу и измерить производительность. Мое решение состоит в том, чтобы создать массив карты глубины.
public $depthMap = [];
public $data = [
['id' => 1, 'name' => 'a', 'iid' => null],
['id' => 2, 'name' => 'b', 'iid' => 1],
['id' => 3, 'name' => 'c', 'iid' => 2],
['id' => 4, 'name' => 'd', 'iid' => 2],
['id' => 5, 'name' => 'e', 'iid' => 3],
['id' => 6, 'name' => 'f', 'iid' => 4],
['id' => 7, 'name' => 'g', 'iid' => 3],
['id' => 8, 'name' => 'h', 'iid' => 5],
['id' => 9, 'name' => 'i', 'iid' => null],
['id' => 10, 'name' => 'j', 'iid' => 7],
['id' => 11, 'name' => 'k', 'iid' => 9],
['id' => 12, 'name' => 'l', 'iid' => 10],
['id' => 13, 'name' => 'm', 'iid' => 4],
['id' => 14, 'name' => 'n', 'iid' => 3],
['id' => 15, 'name' => 'o', 'iid' => 12],
['id' => 16, 'name' => 'p', 'iid' => 10],
];
public function handle()
{
foreach ($this->data as $item) {
$this->depthMap[] = trim($this->getDepth($item) . ".{$item['id']}", '.');
}
}
public function getDepth($item)
{
foreach (array_reverse($this->depthMap) as $mapItem) {
if (array_reverse(explode('.', $mapItem))[0] == $item['iid']) return $mapItem;
}
return '';
}
/* The output
[
"1",
"1.2",
"1.2.3",
"1.2.4",
"1.2.3.5",
"1.2.4.6",
"1.2.3.7",
"1.2.3.5.8",
"9",
"1.2.3.7.10",
"9.11",
"1.2.3.7.10.12",
"1.2.4.13",
"1.2.3.14",
"1.2.3.7.10.12.15",
"1.2.3.7.10.16",
];
*/
Теперь вы можете выполнять операции Expand(), array_reverse(), count() не только для длины глубины, но и для всей карты соотношений.