Как я могу создать «Композитную модель»? и перейти к «составной таблице»;

В настоящее время я изучаю Composite Design Pattern и его использование в Laravel, и я не могу понять несколько вещей.

Допустим, у меня есть список Things, Эти вещи имеют одинаковые базовые свойства, поэтому, если, например, я создавал модель для каждого из Things Я хотел бы получить что-то вроде:

Thing1.php:

<?php
class Thing1 extends Model {
protected $title;
protected $description;

protected $fillable = [
'title',
'description'
];

public function __construct(string $title, string $description) {
$this->title = $title;
$this->description = $description;
}

public function getTitle(): string {
return $this->title;
}

public function getDescription(): string {
return $this->description;
}

public function setTitle(string $title) {
$this->title = $title;
}

public function setDescription(string $description) {
$this->description = $description;
}
}

Thing2.php:

<?php
class Thing2 extends Model {
protected $title;
protected $description;
protected $totalPrice = null;
protected $parent_id;

protected $fillable = [
'title',
'description',
'totalPrice',
'parent_id'
];

public function __construct(string $title, string $description, int $totalPrice = null, int $parent_id) {
$this->title = $title;
$this->description = $description;
$this->totalPrice = $totalPrice;
$this->parent_id = $parent_id;
}

public function getTitle(): string {
return $this->title;
}

public function getDescription(): string {
return $this->description;
}

public function getTotalPrice(): int {
return $this->totalPrice;
}

public function setTitle(string $title) {
$this->title = $title;
}

public function setDescription(string $description) {
$this->description = $description;
}

public function setTotalPrice(int $totalPrice) {
if(!is_null($totalPrice))
$this->totalPrice = $totalPrice;
}

public function setParentId(int $parent_id) {
$this->parent_id = $parent_id;
}
}

Thing3.php:

<?php
class Thing3 extends Model {
protected $title;
protected $description;
protected $totalPrice;

protected $fillable = [
'title',
'description',
'totalPrice'
];

public function __construct(string $title, string $description, int $totalPrice) {
$this->title = $title;
$this->description = $description;
$this->totalPrice = $totalPrice;
}

public function getTitle(): string {
return $this->title;
}

public function getDescription(): string {
return $this->description;
}

public function getTotalPrice(): int {
return $this->totalPrice;
}

public function setTitle(string $title) {
$this->title = $title;
}

public function setDescription(string $description) {
$this->description = $description;
}

public function setTotalPrice(int $totalPrice) {
$this->totalPrice = $totalPrice;
}
}

Но теперь с помощью Composite Design Pattern моя идея — создать модель, которая будет называться ThingComposite.php и будет содержать следующие базовые методы:

<?php
abstract class ThingComposite extends Model {
public function getTitle(): string;
public function getDescription(): string;
public function setTitle(string $title);
public function setDescription(string $description);
}

И 3 вещи модели, которые будут расширяться ThingComposite,

Но я не мог понять несколько вещей, с которыми, я надеюсь, вы могли бы помочь мне:

  1. Если все Things расширяет один класс, который расширяет ThingComposite и иметь отношения с одной таблицей, как я буду в состоянии «арбитраж» между Things?

  2. Свойства: totalPrice а также parent_id являются «специальными ключами» и не отображаются в качестве основных свойств / методов, поскольку они уникальны для конкретной модели. Так как я буду представлять это в модели? и как я могу представить это в таблице?

    Я слышал о JSON в MySQL, может быть, это решение?

  3. Как я могу построить контроллер, если я хочу отобразить каждый Thing своими собственными объектами, но из той же «составной таблицы», которая связана с ThingComposite учебный класс?

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …