Document
Typed document models, annotations, lifecycle and persistence.
Document models define the shape and behavior of application data. Concrete classes extend Core\Document\Base and declare their Collection mapping through PHP attributes.
Basic implementation
The following illustrates the relevant class relationship. Replace the example classes with your application's concrete types.
#[\Core\Document\Annotation\Database(collection: ExampleCollection::class)]
class ExampleDocument extends \Core\Document\Base
{
public string $title = '';
}
Document and Collection mapping
Every concrete Document must extend the Document base and provide a Core\Document\Annotation\Database mapping, directly or through inheritance. The mapping resolves the associated Collection. The Collection separately maps back to its Document type using Core\Collection\Annotation\Database.
Properties and type handling
The base discovers properties through reflection and caches metadata per concrete class. It handles typed, nullable and object properties, BSON conversion, MongoDB ObjectIds and properties originating from inactive document extensions.
Lifecycle and persistence
persist() delegates persistence through the associated Collection. delete() removes the document. Identity is exposed through getId() and setId(); created and updated timestamps have dedicated accessors.
Cloning and export
copy() and __clone() support copying document instances. toArray() and toTemplateData() provide representations for application and rendering use; internal MongoDB conversion is available separately.
Cache annotations
Document annotations Cacheable and NotCacheable control eligibility for document caching. Caching depends on the resolved Collection and database cache controller; annotations do not by themselves guarantee Redis storage.
Public API Reference
Public methods declared by Core\Document\Base. Inherited methods are not repeated here.
Signatures follow the uploaded source implementation.
| Method | Description |
|---|---|
public function __construct() |
Constructor |
public function init(): void |
Initialize document |
public function __get(string $name): mixed |
Magic getter |
public function __set( string $name, mixed $value, ): void |
Magic setter |
public function __clone(): void |
Clone document |
public function copy(): object |
Create document copy |
public function persist(): static |
Persist document |
public function delete(): void |
Delete document |
public function toArray(bool $assoc = false): array |
Convert document to array |
public function toTemplateData(): array |
Convert document to template data |
public function get(string $name): mixed |
Get document property |
public function set( string $name, mixed $value, ): object |
Set document property |
public function getId(bool $asString = false): ObjectId|string|null |
Get MongoDB object ID |
public function setId(ObjectId|string|null $id): static |
Set MongoDB object ID |
public function deleteId(): void |
Delete MongoDB object ID |
public function getCreated(): DateTime|null |
Get creation time |
public function setCreated(DateTime $created): static |
Set creation time |
public function getUpdated(): DateTime|null |
Get update time |
public function setUpdated(DateTime $updated): static |
Set update time |
public function _getMongoDbDocument(): stdClass |
Get MongoDB document |
public function _getExtendedProperties(): array |
Get properties from inactive document extensions |
public function _getCollectionClassName(): string |
Get mapped collection class name |
public function _getCollection(): object |
Get mapped collection |
public function _getMongoDbCollectionName(): string |
Get MongoDB collection name |
public function _getMongoDbCollection(): object |
Get MongoDB collection |
public function _getProperties(): array |
Get reflected document property metadata |
public function _getCacheable(): bool |
Get document cacheable state |