Entities and Mapping
Entity Classes
Repository creates an entity from each selected row using the support package’s hydration facilities. stdClass provides public fields; Derafu\Repository\Entity provides generic attributes and accessor methods. Application entity classes can provide their own properties and accessors.
The default data-provider factory’s entity class is Derafu\Repository\Entity. The factory implements ConfigurableInterface; its configuration accepts entityClass and repositoryClass for the in-memory construction path:
use Derafu\Repository\Entity;
use Derafu\Repository\Repository;
$factory->setConfiguration([
'entityClass' => Entity::class,
'repositoryClass' => Repository::class,
]);
Repository Identifiers
For the default data-provider factory:
- An identifier containing a namespace separator is resolved as an entity class or entity interface convention.
- An identifier without a namespace separator uses the configured default entity class.
- An interface name ending in
Interfaceis mapped from theContractnamespace segment toEntity, with the suffix removed. For example,App\Contract\ProductInterfacemaps toApp\Entity\Product.
The source key must match the identifier passed to getRepository(). If the source is registered under ProductInterface::class, request that identifier rather than the concrete entity class.
Explicitly registered factories receive their identifier unchanged and do not use these entity naming conventions.
Specialized In-Memory Repositories
Associate an entity with a repository subclass through the Mapping\Entity attribute:
namespace App\Entity;
use App\Repository\ProductRepository;
use Derafu\Repository\Entity as GenericEntity;
use Derafu\Repository\Mapping as DEM;
#[DEM\Entity(repositoryClass: ProductRepository::class)]
class Product extends GenericEntity
{
}
The subclass can add domain-specific queries while retaining the in-memory implementation:
namespace App\Repository;
use App\Entity\Product;
use Derafu\Repository\Repository;
/**
* @extends Repository<Product>
*/
class ProductRepository extends Repository
{
/**
* @return Product[]
*/
public function findComputers(): array
{
return $this->findBy(['category' => 'computers']);
}
}
@extends Repository<Product> states the entity of the subclass, so that static analysis types its query methods as Product. The entity class that the default factory passes to the constructor must be that same class.
Register App\Entity\Product::class as a data-provider source key. When requested, the default factory reads the attribute and constructs ProductRepository with the normalized data, entity class, ID attribute and injected criteria evaluator. Without a repository class in the attribute, the configured default repository class is used.
The selected class must be an instantiable Repository subclass compatible with those constructor arguments. For repositories requiring a connection or other construction dependencies, register a factory instead.
The mapping attribute also exposes a readOnly metadata flag. The built-in manager and repository do not enforce it as a write restriction.
Enum Entities
A backed enum can be the entity class, causing query methods to return enum cases:
use Derafu\Repository\Repository;
$repository = new Repository(
[
'active' => ['id' => 'active'],
'inactive' => ['id' => 'inactive'],
],
Status::class,
idAttribute: 'id',
);
$active = $repository->find('active'); // Status::Active.
Here Status is the backed enum from the data sources guide. The configured ID field must contain an integer or string accepted by the enum’s from() method. A missing ID field is filled from the row key when idAttribute is configured. Invalid values and unsupported enum entities raise EntityException.
With a manager, register a namespaced enum class as the source key and use 'enum:' . Status::class as its source value to combine enum reading with enum entities.
Generic Attribute Access
Entity supports getAttribute(), setAttribute(), hasAttribute(), unsetAttribute() and toArray(), along with magic properties and getXyz() / setXyz() accessors.
An explicitly stored null is an existing attribute. unsetAttribute('name') removes the attribute, whereas unset($entity->name) stores null under that name. Reading a missing attribute or invoking an unsupported method raises EntityException.
Typed Attribute Reading
EntityInterface::getReader() returns a Derafu\Array\ArrayReader. The default Entity implementation creates a snapshot of its attributes on each call. Changes made later through setAttribute() or unsetAttribute() appear in a new reader; existing readers retain their snapshot.
use Derafu\Repository\Entity;
$entity = new Entity();
$entity->setAttribute('id', 7)
->setAttribute('amount', '19.90')
->setAttribute('profile', ['name' => 'Ana'])
->setAttribute('optional', null);
$reader = $entity->getReader();
$id = $reader->requireInt('id'); // 7.
$amount = $reader->requireAsFloat('amount'); // 19.9.
$name = $reader->requireString('profile.name'); // 'Ana'.
$exists = $reader->hasKey('optional'); // true.
$hasValue = $reader->hasValue('optional'); // false.
Strict methods such as requireInt() reject numeric strings; explicit conversion methods such as requireAsFloat() use the conversion rules of Derafu Value. Dot paths navigate nested arrays, with an existing literal key taking precedence over navigation. Reading does not change the stored attributes or reindex their arrays.
Reader failures use the exceptions from Derafu Array, while getAttribute() retains its EntityException contract. Custom classes that implement EntityInterface must also implement getReader(). See typed array readers for optional reads, required values, conversions and reader exceptions.