1. Go to this page and download the library: Download earc/data library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$entity = new MyEntity('some arguments');
data_persist($entity); // saves the entity
$pk = $entity->getPrimaryKey(); // yields the primary key - may be set prior persistence
data_load(MyEntity::class, $pk); // returns the entity of MyEntity::class with the primary key $pk
data_delete($entity); // removes the entity
data_remove(MyEntity::class, $pk); // removes the entity without the need of being loaded before
data_find(MyEntity::class, ['name' => ['Anton', 'Max', 'Simon'], 'age' => 42]); // returns the primary keys for all MyEntity instances where the name property is equal to 'Anton', 'Max' or 'Simon' and the age property is 42
// expert functions - use only if you really know what you are doing
data_schedule($entity); // schedules the saving process until `data_persist` is called with any argument
data_detach(get_class($entity), [$pk]); // removes the entity from the earc/data entity cache
use eArc\Data\Entity\AbstractEntity;
class MyReverencedEntity extends AbstractEntity {/*...*/}
class Entity extends AbstractEntity
{
protected string $myReverencedEntityPK;
public function setMyReverencedEntity(MyReverencedEntity $myReverencedEntity)
{
$this->myReverencedEntityPK = $myReverencedEntity->getPrimaryKey();
}
public function getMyReverencedEntity(): MyReverencedEntity
{
return data_load(MyReverencedEntity::class, $this->myReverencedEntityPK);
}
}
use eArc\Data\Collection\Collection;
use eArc\Data\Entity\AbstractEntity;
class MyReverencedEntity extends AbstractEntity {/*...*/}
class EntityWithCollection extends AbstractEntity
{
protected Collection $myReverencedEntityCollection;
public function __construct()
{
$this->myReverencedEntityCollection = new Collection($this, MyReverencedEntity::class);
}
public function getMyReverencedEntityCollection(): Collection
{
return $this->myReverencedEntityCollection;
}
}
$entityWithCollection = new EntityWithCollection();
$myReverencedEntity = new MyReverencedEntity();
$collection = $entityWithCollection->getMyReverencedEntityCollection();
$collection->add($myReverencedEntity->getPrimaryKey());
$collection->remove($myReverencedEntity->getPrimaryKey());
foreach ($collection as $primaryKey) {
echo $primaryKey;
}
foreach ($collection->asArray() as $entity) {
echo $entity->getPrimaryKey();
}
use eArc\Data\Entity\AbstractEntity;
use eArc\Data\Entity\AbstractEmbeddedEntity;
class MyEmbeddedEntity extends AbstractEmbeddedEntity
{
//...
public function setParent(MyRootEntity $parent)
{
$this->ownerEntity = $parent;
}
}
class MyRootEntity extends AbstractEntity
{
protected MyEmbeddedEntity $myEmbeddedEntity;
public function setMyReverencedEntity(MyEmbeddedEntity $myEmbeddedEntity)
{
$this->myEmbeddedEntity = $myEmbeddedEntity;
$myEmbeddedEntity->setParent($this);
}
public function getMyEmbeddedEntity(): MyEmbeddedEntity
{
return $this->myEmbeddedEntity;
}
}
use eArc\Data\Collection\EmbeddedCollection;
use eArc\Data\Entity\AbstractEntity;
use eArc\Data\Entity\AbstractEmbeddedEntity;
class MyEmbeddedEntity extends AbstractEmbeddedEntity {/*...*/}
class MyRootEntity extends AbstractEntity
{
protected EmbeddedCollection $myEmbeddedEntityCollection;
public function __construct()
{
$this->myEmbeddedEntityCollection = new EmbeddedCollection($this, MyEmbeddedEntity::class);
}
public function getMyEmbeddedEntityCollection(): EmbeddedCollection
{
return $this->myEmbeddedEntityCollection;
}
}
$rootEntity = new MyRootEntity();
$myEmbeddedEntity = new MyEmbeddedEntity();
$collection = $rootEntity->getMyEmbeddedEntityCollection();
$collection->add($myEmbeddedEntity);
$collection->remove($myEmbeddedEntity);
foreach ($collection as $entity) {
echo $entity::class;
}
foreach ($collection->asArray() as $entity) {
echo $entity::class;
}
use eArc\Data\Entity\AbstractEntity;
use eArc\Data\Entity\Interfaces\EntityInterface;
use \eArc\Data\Entity\Interfaces\Events\PrePersistInterface;
class MyReverencedEntity extends AbstractEntity {/*...*/}
class SomeEntity extends AbstractEntity implements PrePersistInterface
{
protected string $myReverencedEntityPK;
public function setMyReverencedEntity(MyReverencedEntity $myReverencedEntity)
{
$this->myReverencedEntityPK = $myReverencedEntity->getPrimaryKey();
}
public function getMyReverencedEntity(): MyReverencedEntity
{
return data_load(MyReverencedEntity::class, $this->myReverencedEntityPK);
}
public function prePersist(EntityInterface $entity): void
{
if ($entity = data_load(MyReverencedEntity::class, $this->myReverencedEntityPK, true)) {
data_persist($entity);
}
}
}
use eArc\Data\Entity\Interfaces\Events\PreRemoveInterface;
di_tag(PreRemoveInterface::class, MyPreRemoveService::class); // <- this comes in your bootstrap section
class MyPreRemoveService implements PreRemoveInterface
{
public static function preRemove(string $fQCN, string $primaryKey): void
{
// pre remove logic goes here...
}
}
use eArc\Data\Entity\Interfaces\MutableEntityReferenceInterface;
use eArc\Data\Entity\Interfaces\PrimaryKey\MutableReverenceKeyInterface;
use eArc\Data\Entity\Interfaces\ImmutableEntityInterface;
use eArc\Data\Entity\AbstractEntity;
use eArc\Data\Exceptions\DataException;
class MyClassReverencingAImmutable extends AbstractEntity implements MutableEntityReferenceInterface
{
protected string|null $myImmutablePK;
//...
public function setMutableReverenceTarget(MutableReverenceKeyInterface $entity): void
{
$this->myImmutablePK = $entity->getPrimaryKey();
}
public function getMyImmutable(): MyImmutable|null
{
return is_null($this->myImmutablePK) ? null : data_load(MyImmutable::class, $this->myImmutablePK);
}
public function setMyImmutable(MyImmutable $immutable)
{
// If the current class does not have a primary key yet, we have to persist it first.
// A post persist event does not reduce the effort and may lead into a infinity loop.
// You may use the solution for reverences between two immutables to circumvent this.
if (is_null($this->primaryKey)) {
throw new DataException('Primary key is missing. Try to persist this entity first.');
}
// The reverse key is set on persist.
$immutable->setMutableReverenceKey($this->primaryKey);
}
//...
}
class MyImmutable extends AbstractEntity implements MutableReverenceKeyInterface, ImmutableEntityInterface
{
protected string|null $mutableReferencePrimaryKey;
//...
public function getMutableReverenceKey(): string
{
return $this->mutableReferencePrimaryKey;
}
public function setMutableReverenceKey(string $mutableReferencePrimaryKey): void
{
$this->mutableReferencePrimaryKey = $mutableReferencePrimaryKey;
}
public function getMutableReverenceClass(): string
{
return MyClassReverencingAImmutable::class;
}
//...
}
class ReferenceUsingProxy extends AbstractEntity
{
protected string $myImmutableProxyFQCN;
protected string $myImmutableProxyPK;
//...
public function setMyImmutable(MyImmutable $immutable)
{
$this->myImmutableProxyFQCN = $immutable->getMutableReverenceClass();
$this->myImmutableProxyPK = $immutable->getMutableReverenceKey();
}
public function getMyImmutable(): MyImmutable
{
$proxy = data_load($this->myImmutableProxyFQCN, $this->myImmutableProxyPK)->getMyImmutable();
return $proxy->getMyImmutable();
}
//...
}
use eArc\Data\Entity\Interfaces\PrimaryKey\MutableReverenceKeyInterface;
use eArc\Data\Entity\Interfaces\ImmutableEntityInterface;
use eArc\Data\Entity\AbstractEntity;
use eArc\Data\Entity\GenericMutableEntityReference;
class MyImmutableClassReverencingAImmutable extends AbstractEntity
{
protected string|null $myImmutableLinkPK;
//...
public function getMyImmutable(): MyImmutable|null
{
return is_null($this->myImmutableLinkPK) ? null :
data_load(GenericMutableEntityReference::class, $this->myImmutableLinkPK)->getMutableReverenceTarget();
}
public function setMyImmutable(MyImmutable $immutable)
{
$this->myImmutableLinkPK = $immutable->getMutableReverenceKey();
}
//...
}
class MyImmutable extends AbstractEntity implements MutableReverenceKeyInterface, ImmutableEntityInterface
{
protected string|null $mutableReferencePrimaryKey;
public function __construct(GenericMutableEntityReference|null $mutableReference)
{
if (is_null($mutableReference)) {
$mutableReference = new GenericMutableEntityReference();
}
if (is_null($mutableReference->getPrimaryKey())) {
data_persist($mutableReference);
}
$this->mutableReferencePrimaryKey = $mutableReference->getPrimaryKey();
}
//...
public function getMutableReverenceKey(): string
{
return $this->mutableReferencePrimaryKey;
}
public function getMutableReverenceClass(): string
{
return GenericMutableEntityReference::class;
}
//...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.