Repository navigation
RecordMapper
Pair\Data\RecordMapper converts persistence records into explicit Pair v4 read models.
It is intentionally strict: the target class must exist, implement ReadModel, and implement MapsFromRecord.
use Pair\Data\RecordMapper;
$customer = new Customer(7);
$readModel = RecordMapper::map($customer, CustomerReadModel::class);
return new \Pair\Http\JsonResponse($readModel);JsonResponse will call toArray() because the mapped object implements ReadModel.
use Pair\Data\ArraySerializableData;
use Pair\Data\MapsFromRecord;
use Pair\Data\ReadModel;
use Pair\Orm\ActiveRecord;
/**
* Public account payload used by API responses.
*/
final readonly class AccountReadModel implements ReadModel, MapsFromRecord {
use ArraySerializableData;
/**
* Build the account payload.
*/
public function __construct(
public int $id,
public string $name,
public bool $active
) {}
/**
* Build the account payload from the persistence model.
*/
public static function fromRecord(ActiveRecord $record): static {
return new self(
(int)$record->id,
(string)$record->name,
(bool)$record->active
);
}
/**
* Export the public account payload.
*
* @return array<string, mixed>
*/
public function toArray(): array {
return [
'id' => $this->id,
'name' => $this->name,
'active' => $this->active,
];
}
}use Pair\Data\RecordMapper;
$rows = [];
foreach (Account::query()->where('active', 1)->get() as $account) {
$rows[] = RecordMapper::map($account, AccountReadModel::class)->toArray();
}
return \Pair\Api\ApiResponse::jsonResponse(['data' => $rows]);For CRUD resources, CrudController runs this mapping internally when readModel is configured.
RecordMapper::map() throws InvalidArgumentException when:
- the read-model class does not exist
- the class does not implement
Pair\Data\ReadModel - the class does not implement
Pair\Data\MapsFromRecord
This keeps bad API configuration visible during development instead of silently falling back to persistence serialization.
See also: ReadModel, MapsFromRecord, Payload, CrudController, SearchIndexableReadModel.
Maintained by Viames Marino — VM Ingegneria · Professional enquiries
Home · Setup · Configuration
Build with Pair 4
- Generator
- Controllers and responses
- Read models
- ActiveRecord and queries
- Forms and validation
- PairUI.js and HTML fragments
APIs and authentication
- API guide and CRUD
- Middleware and rate limits
- Users, sessions, and ACL
- Passkeys and social login
- Native mobile setup
Integrate and operate
- Integrations
- Runtime extensions and adapter keys
- Cache and cache stores
- Logs and observability
- PWA and push
- Packages and distribution
- Upgrade to v4