Extending a model erases relations instead of merging them #314
-
When you have a model use Winter\Storm\Database\Model;
class Foo implements Model {
public $table = 'foo';
public $belongsTo = ['bar' => Bar::class];
}
class Baz extends Foo {
public $belongsTo = ['bal' => Bal::class];
} if you do a new Baz() it will have only one property in the $belongsTo and that is I would like it when there would be a trait or something that makes a model inherit the relationships of the "parent" classes when so chosen. By using I could write a behaviour for this, but would such a feature be welcome and what should it be named? All I can think of is |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Just merge the parent's relation property in the constructor instead. |
Beta Was this translation helpful? Give feedback.
-
Something like:
|
Beta Was this translation helpful? Give feedback.
-
I was going to suggest exactly that @LukeTowers |
Beta Was this translation helpful? Give feedback.
-
I ended up writing my own little utility library, that can be used by anyone that wishes to transport relationships defined in the parents to the current instance, by adding one class to the https://github.com/tschallacka/storm-inherit-relations composer require tschallacka/storm-inherit-relations ^1.0 UsageIn a model in which you wish to glean the relationships defined in public $implement = [\Tschallacka\StormInheritRelations\Behavior\InheritRelations]; Model example: <?php
namespace Tschallacka\Example\Attribute;
use Winter\Storm\Database\Model;
use Tschallacka\StormInheritRelations\Behavior\InheritRelations;
class AttributeValue extends Model
{
public $primaryKey = 'value_id';
public $implement = [
InheritRelations::class
];
public $belongsTo = [
'attribute' => AttributeName::class,
];
} |
Beta Was this translation helpful? Give feedback.
I ended up writing my own little utility library, that can be used by anyone that wishes to transport relationships defined in the parents to the current instance, by adding one class to the
$implement
array.https://github.com/tschallacka/storm-inherit-relations
Usage
In a model in which you wish to glean the relationships defined in
the parents add this to your
$implements
array:Model example: