Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relationships: make HasOne relationship lazy until entity is attached #467

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/entity.texy
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ echo $member->hasValue('web') ? 'has web' : '-';
$member->isPersisted(); // false
\--

Attaching entities to the repository is letting Orm know about your entities, it does not store the entity. Attaching to repository injects the required dependencies into your entity (through inject property annotations or inject methods). If you need some dependency before attaching entity to the repository, feel free to pass the dependency through the constructor, which is by default empty.
Attaching entities to the repository lets Orm know about your entities. Attaching to a repository runs the required dependencies injection into your entity (through inject property annotations or inject methods). If you need some dependency before attaching entity to the repository, feel free to pass the dependency via the constructor, which is by default empty.

Each entity can be created "manually". Entities can be simply connected together. Let's see an example:

Expand All @@ -54,7 +54,7 @@ $book->author = $author;
$book->tags->set([new Tag(), new Tag()]);
\--

If a instance Author is attached to the repository, all other new connected entities are automatically attached to their repositories too. See more in [relationships chapter | relationships].
If an Author instance is attached to the repository, all other new connected entities are automatically attached to their repositories too. See more in [relationships chapter | relationships].

-----------

Expand Down
46 changes: 28 additions & 18 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function __clone()
{
$id = $this->hasValue('id') ? $this->getValue('id') : null;
$persistedId = $this->persistedId;
$isAttached = $this->isAttached();
foreach ($this->getMetadata()->getProperties() as $name => $metadataProperty) {
// getValue loads data & checks for not null values
if ($this->hasValue($name) && is_object($this->data[$name])) {
Expand All @@ -229,18 +230,27 @@ public function __clone()
$this->data['id'] = null;
$this->persistedId = null;
$this->data[$name] = clone $this->data[$name];
$this->data[$name]->setPropertyEntity($this);
$this->data[$name]->onEntityAttach($this);
if ($isAttached) {
$this->data[$name]->onEntityRepositoryAttach($this);
}
$this->data[$name]->set($data);
$this->data['id'] = $id;
$this->persistedId = $persistedId;

} elseif ($this->data[$name] instanceof IRelationshipContainer) {
$this->data[$name] = clone $this->data[$name];
$this->data[$name]->setPropertyEntity($this);
$this->data[$name]->onEntityAttach($this);
if ($isAttached) {
$this->data[$name]->onEntityRepositoryAttach($this);
}

} elseif ($this->data[$name] instanceof EmbeddableContainer) {
$this->data[$name] = clone $this->data[$name];
$this->data[$name]->setPropertyEntity($this);
$this->data[$name]->onEntityAttach($this);
if ($isAttached) {
$this->data[$name]->onEntityRepositoryAttach($this);
}

} else {
$this->data[$name] = clone $this->data[$name];
Expand Down Expand Up @@ -309,8 +319,18 @@ public function onFree(): void

public function onAttach(IRepository $repository, EntityMetadata $metadata): void
{
$this->attach($repository);
if ($this->isAttached()) {
return;
}

$this->repository = $repository;
$this->metadata = $metadata;

foreach ($this->data as $property) {
if ($property instanceof IEntityAwareProperty) {
$property->onEntityRepositoryAttach($this);
}
}
}


Expand Down Expand Up @@ -499,22 +519,12 @@ private function createPropertyWrapper(PropertyMetadata $metadata): IProperty
assert($wrapper instanceof IProperty);

if ($wrapper instanceof IEntityAwareProperty) {
$wrapper->setPropertyEntity($this);
$wrapper->onEntityAttach($this);
if ($this->isAttached()) {
$wrapper->onEntityRepositoryAttach($this);
}
}

return $wrapper;
}


/**
* @param IRepository<IEntity> $repository
*/
private function attach(IRepository $repository): void
{
if ($this->repository !== null && $this->repository !== $repository) {
throw new InvalidStateException('Entity is already attached.');
}

$this->repository = $repository;
}
}
4 changes: 3 additions & 1 deletion src/Entity/Embeddable/Embeddable.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public function onAttach(IEntity $entity): void
}




/**
* @return mixed
*/
Expand Down Expand Up @@ -144,7 +146,7 @@ private function createPropertyWrapper(PropertyMetadata $metadata): IProperty
if ($this->parentEntity === null) {
throw new InvalidStateException("");
} else {
$wrapper->setPropertyEntity($this->parentEntity);
$wrapper->onEntityAttach($this->parentEntity);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/Entity/Embeddable/EmbeddableContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ public function __construct(PropertyMetadata $propertyMetadata)
}


public function setPropertyEntity(IEntity $entity): void
public function onEntityAttach(IEntity $entity): void
{
$this->entity = $entity;
}


public function onEntityRepositoryAttach(IEntity $entity): void
{
}


public function convertToRawValue($value)
{
return $value;
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/Embeddable/IEmbeddable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Nextras\Orm\Entity\IEntity;


/**
* @experimental This interface API is experimental and is subjected to change. It is ok to use its implementation.
*/
interface IEmbeddable
{
/**
Expand Down
14 changes: 13 additions & 1 deletion src/Entity/IEntityAwareProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
namespace Nextras\Orm\Entity;


/**
* @experimental This interface API is experimental and is subjected to change. It is ok to use its implementation.
*/
interface IEntityAwareProperty extends IProperty
{
public function setPropertyEntity(IEntity $entity): void;
/**
* this listener is ired when property is attached to entity.
*/
public function onEntityAttach(IEntity $entity): void;


/**
* This listener is fired when the entity is attached to repository.
*/
public function onEntityRepositoryAttach(IEntity $entity): void;
}
13 changes: 7 additions & 6 deletions src/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ public function __construct(PropertyMetadata $metadata)
}


/**
* @internal
* @ignore
*/
public function setPropertyEntity(IEntity $parent): void
public function onEntityAttach(IEntity $entity): void
{
$this->parent = $entity;
}


public function onEntityRepositoryAttach(IEntity $entity): void
{
$this->parent = $parent;
}


Expand Down
Loading