Stratified entities and configuration¶
Entity contract¶
Every stratified entity must:
- extend
AbstractEntity; - implement
StratifiedEntityInterface; - declare an
idfield as a primary key; - expose a mapped
buildStratefield; - expose the configured scope-id field;
- be managed by an
AbstractRelationnalManager.
The provided trait supplies buildStrate, accessors and manual primary-key persistence.
<?php
declare(strict_types=1);
namespace App\Entity;
use Small\SwooleEntityManager\Entity\AbstractEntity;
use Small\SwooleEntityManager\Entity\Attribute\Field;
use Small\SwooleEntityManager\Entity\Attribute\OrmEntity;
use Small\SwooleEntityManager\Entity\Attribute\PrimaryKey;
use Small\SwooleEntityManager\Entity\Enum\FieldValueType;
use Small\SwooleEntityManager\Strates\Contract\StratifiedEntityInterface;
use Small\SwooleEntityManager\Strates\Entity\StratifiedEntityTrait;
#[OrmEntity]
final class BookingItemEntity extends AbstractEntity implements StratifiedEntityInterface
{
use StratifiedEntityTrait;
#[PrimaryKey]
private ?string $id = null;
#[Field(type: FieldValueType::int)]
private ?int $bookingId = null;
#[Field(type: FieldValueType::string)]
private ?string $sku = null;
}
StratifiedEntityTrait includes ManualPrimaryKeyPersistenceTrait. It preserves non-null application-generated keys in insert payloads while keeping null auto-generated keys omitted.
When buildStrate is declared manually, also use ManualPrimaryKeyPersistenceTrait and implement the interface accessors.
Manager¶
#[Connection(dbTableName: 'booking_item')]
#[Entity(BookingItemEntity::class)]
final class BookingItemManager extends AbstractRelationnalManager
{
}
Configure scopes¶
use Small\SwooleEntityManager\Strates\Config\StratifiedEntityConfig;
use Small\SwooleEntityManager\Strates\Service\StratifiedPersist;
$stratifiedPersist = new StratifiedPersist(
$entityManagerFactory,
[
new StratifiedEntityConfig(
managerClass: BookingItemManager::class,
scope: 'booking',
scopeIdField: 'bookingId',
scopeRoot: true,
),
],
);
Constructor validation checks:
- duplicate manager registration;
- non-empty scope and scope-id field;
- manager inheritance;
- required
id,buildStrateand scope-id fields; - implementation of
StratifiedEntityInterface; - at most one explicit root manager per scope.
When no manager is explicitly marked scopeRoot: true, the first manager registered for the scope remains the root for backward compatibility.