Replies: 1 comment
-
I tried to create a new lookup: <?php
class Country {
private $table = 'countries';
public function getTable(): string
{
return $this->table;
}
public function hasManyThrough(string $related, string $localKey, string $foreignKey, array $relations)
{
$relatedTable = (new $related)->getTable();
$query = "SELECT $relatedTable.* FROM $relatedTable";
$latesTable = null;
foreach ($relations as $model => $relation) {
$relationTable = (new $model)->getTable();
$query .= " INNER JOIN $relationTable ON $relationTable.{$relation['local_key']} = " . ($latestTable ?? $relatedTable) . '.' . $relation['foreign_key'];
$latestTable = $relationTable;
}
$table = $this->getTable();
$query .= " INNER JOIN $table ON $table.$localKey = $latestTable.$foreignKey";
return $query;
}
}
class Region {
private $table = 'regions';
public function getTable(): string
{
return $this->table;
}
}
class City {
private $table = 'cities';
public function getTable(): string
{
return $this->table;
}
}
class District {
private $table = 'districts';
public function getTable(): string
{
return $this->table;
}
}
class Classified {
private $table = 'classifieds';
public function getTable(): string
{
return $this->table;
}
}
$test = (new Country)->hasManyThrough(Classified::class, 'uuid', 'country_uuid', [
District::class => [
'local_key' => 'uuid',
'foreign_key' => 'district_uuid',
],
City::class => [
'local_key' => 'uuid',
'foreign_key' => 'city_uuid',
],
Region::class => [
'local_key' => 'uuid',
'foreign_key' => 'region_uuid',
],
]);
var_dump($test);
// output: SELECT classifieds.* FROM classifieds INNER JOIN districts ON districts.uuid = classifieds.district_uuid INNER JOIN cities ON cities.uuid = districts.city_uuid INNER JOIN regions ON regions.uuid = cities.region_uuid INNER JOIN countries ON countries.uuid = regions.country_uuid |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I know there is already a package for deep relations.
But I think the framework needs a similar method.
Example:
Country -> Region -> City -> District -> Classified
OR:
Beta Was this translation helpful? Give feedback.
All reactions