Objectiphy Mapping
Mapping definition classes which can be loaded from either attributes or annotations.
Last updated
Mapping definition classes which can be loaded from either attributes or annotations.
Last updated
To use Objectiphy mapping definitions, include a use statement at the top of your entity class. There are three attributes or annotations available: @Table
, @Column
, and @Relationship
:
The Objectiphy attributes and annotations support the following attributes:
Use this on the class itself (not the properties), to identify the table name, and perhaps a custom repository and whether to insist that the custom repository is always used.
name: Name of the main table that stores information for this entity.
repositoryClassName: Name of custom repository class to use when loading this entity.
alwaysLateBind: Whether or not to force even eager loaded one-to-one and many-to-one relationships to late bind so that they can use the custom repository. Defaults to false. Only specify true here if absolutely necessary, as it will require a separate database call to populate this entity wherever it appears (it cannot be eager loaded with joins).
Use a column annotation for properties that represent scalar values (are not arrays or objects other than DateTimeInterface
types) - otherwise, use a relationship annotation (see below). You don't necessarily have to specify any of these attributes - name can be guessed, type will default to string, and the rest would only apply to certain properties (eg. the primary key). So you could just use @Column
on its own for most scalar properties.
name: Name of the column that stores data for this property. You can use the special value 'IGNORE' to skip over this property completely. If you don't specify a name, Objectiphy can guess the name of the column based on a naming strategy (by default, pascal or camel to snake case).
type: Data type name. If the property has a data type, that is not a string, you should also populate this attribute. Built-in data types that are supported are as follows (note, you can also implement your own data types and conversions by injecting a custom class implementing DataTypeHandlerInterface to the repository factory):
format: A format string to use with DateTimes or sprintf.
If the value is a DateTime
or DateTimeImmutable
, it can be converted to a formatted string by specifying the format here. If the value is a string, it can be processed with sprintf
by specifying a pattern here (and the value will be the argument). NOTE: If you want to round a number that is stored in the database as a decimal to 2 decimal places, mark the property as a string, and use %0.2f
as the format. This will prevent unexpected issues with floating point precision conversions.
isPrimaryKey: Boolean flag indicating primary key.
autoIncrement: Whether or not the primary key value auto increments.
Only applies if isPrimaryKey
is set to true, and defaults to true. If set to false, you must supply the primary key value yourself when inserting, and Objectiphy will insert if possible, or update if the key already exists.
isReadOnly: Whether or not to prevent this field being persisted. If null (which is the default), this will evaluate to false for everything EXCEPT scalar joins (it is safer to assume scalar joins are read-only as they are usually used to look up a value in a cross-reference table), or properties that use aggregate functions (as these are calculated based on other data, they cannot be set arbitrarily - more on these below). You can override the default behaviour by specifying a value of either true or false on the mapping definition.
aggregateFunctionName: Name of aggregate function to use to calculate the value of this property. Eg. "AVG", "SUM", "MAX". If an aggregate function is specified, the value of the property is always read-only.
aggregateCollectionPropertyName: Name of the to-many property that holds the collection of records on which to perform the aggregate function. This should be the name of a property on the same object, and should hold a collection of child entities (ie. have a one-to-many or many-to-many relationship with the parent class).
aggregatePropertyName: Name of the property on the child collection to pass as a parameter to the aggregate function, if applicable. This should be the name of a property on the child class that is the subject of the aggregate function. If not specified, the primary key of the child class will be used.
aggregateGroupBy: Name of the property to group by, if not the primary key of the class this annotation appears on.