You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the newly added feature, maybe, there is a new way how to write entities.
Consequently, this would utilize property types, annotations & property hooks as well.
class Author extends Entity
{
#[Primary]
publicint|null$id;
publicstring$name;
#[Default("2021-03-21 08:23:00")]
publicDateTimeImmutable|null$born;
publicstring$web = "http://www.example.com";
#[ManyHasOne(Author::class, "favoredBy")]
publicAuthor|null$favoriteAuthor;
#[OneHasMany(Author::class, "favoriteAuthor")]
publicOneHasMany$favortedBy;
#[OneHasMany(Book::class, 'author', orderBy: ['id' => 'desc'], cascade: ['persist', 'remove'])
public OneHasMany $books;
#[OneHasMany(Book::class, 'translator')
public OneHasMany $translatedBooks;
#[OneHasMany(TagFollower::class, 'author', cascade: ['persist', 'remove'])
public OneHasMany $tagFollowers;
publicint$age {
get {
if ($this->born === null) return0;
return ((int) date('Y')) - ((int) $this->born->format('Y'));
}
}
// example of still public/open constructorpublic__construct(string $name) {
parent::__construct();
$this->name = $name;
}
}
Implementation comments:
*HasOne relationships's backing relationship instance would have to be stored somewhere else, i.e. property wrappers have to be separated into user-facing and backing field ones and the second has to be handled separately.
There is still an open question of how "setting" a hasOne relationship could properly propagate to its backing field relationship; a required setter with sideffect? or utilize hooks to avoid hidden backing-fields?
The whole mechanism could be implemented as another option and it would be up to the user when to migrate to this new way of entity definition.
old getters/setters would be deprecated
HasOne options:
class Author {
#[ManyHasOne(Author::class, "favoredBy")]
privateManyHasOne$favoriteAuthorRel;
publicAuthor|null$favoriteAuthor { get => $this->favoriteAuthorRel->getEntity(); }
}
or
class Author {
#[ManyHasOne(Author::class, "favoredBy")]
publicAuthor|null$favoriteAuthor {
set {
$this->favoriteAuthor = $value;
$this->updateReverse('...', $value);
}
}
The text was updated successfully, but these errors were encountered:
This is technically possible even without PHP 8.4. It needs a few hacks, but I managed to create quite clear implementation in orisai/object-mapper. I can help with backporting it if it's welcome. My current project will be stuck on PHP 7.4 for a while, so I can't test new cool stuff...
Btw, there should be a way to distinguish between mapped property and regular property - for dependencies and memory cache.
With the newly added feature, maybe, there is a new way how to write entities.
Consequently, this would utilize property types, annotations & property hooks as well.
So the original entity like:
would become
Implementation comments:
HasOne options:
or
The text was updated successfully, but these errors were encountered: