Consider the node chart below:
As shown in the image above, I want to create a function that can tell me the depth of a relationship from the top-level node.
Instance Model
/**
* 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
}
Instance Table
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');
});
Instance table example
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
In a phrase, my problem is: "If an instance has a parent instance, add 1. Repeat until the parent instance does not have a parent instance. Then return the final value."
How can I accomplish this properly in Laravel?
CodePudding user response:
My solution isn't using recursion, and I don't know how it will perform with a large data. I don't think it will be an issue, but I suggest you seed a huge table and measure the performance. My solution is to create an array of depth map.
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",
];
*/
Now, you can make explode(), array_reverse(), count() operations to now not only the length of the depth but also the whole releation map.
CodePudding user response:
Below is the solution I came up with:
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;
}
}
Instance Model
/**
* Get the depth of this instance from the top-level instance.
*/
public function getDepthAttribute()
{
return DepthHelper($this->id);
}
protected array $appends = ['depth'];