Relations
#
HasOneThe Has One relation defines that an entity exclusively owns another entity in a form of parent-child. Consider this relation as a form of decomposition with the ability to store data in external table.
The HasOne relation is used to define the relation to one child object. This object will be automatically be saved with its parent. The simplest form of relation definition.
To define a HasOne relation, use the HasOne
attribute.
#
HasManyThe Has Many relations defines that an entity exclusively owns multiple other entities in a form of parent-children.
To define a Has Many relation use:
#
BelongsToBelongs To relation defines that an entity is owned by a related entity on the exclusive matter.
Example: a post belongs to an author, a comment belongs a post. Most belongsTo
relations can be created using the inverse
option of the declared hasOne
or hasMany
relation.
The entity will always be persisted after its related entity.
You must properly handle the cases when the related entity is
null
.
By default, the ORM will generate an outer key in the relation object using the related entity's table and outer key ( primary key by default) values. As result column and FK will be added to Review entity on user_id column.
#
RefersToThe RefersTo
relation is similar to the BelongsTo
relation, but must be used to establish multiple relations to the same entity (or in case of a cyclic relation).
You must properly handle the cases when the related entity is
null
.
By default, the ORM will generate an outer key in the relation object using the related entity's table and outer key ( primary key by default) values. As result column and FK will be added to Review entity on user_id column.
#
ManyToManyA relation of type 'Many To Many' provides a more complex connection. This relation will create a junction table to store the relation. This table will be generated automatically.
Optionally you add the movies to the actors entity. There are no attributes necessary, as the relation is already defined in the movie
entity.
#
Embedding EntitiesThe ORM can simplify the definition of large entities by providing the ability to split some columns into an embedded entity. Embedded entities by default will always be loaded with the parent object. However, partial entity selection is possible as well.
Embedded entities do not support relations at the moment.
#
DefinitionTo define an embeddable entity use the #[Embeddable]
(Swift\Orm\Attributes\Embeddable) attribute. As with #[Entity]
.
You do not need to define the primary column, this column will be inherited from the parent entity.
To embed an entity use the #[Embedded]
(Swift\Orm\Attributes\Embedded) attribute. Do not forget to initiate the embedding in your entity (see constructor).
#
Column mappingBy default, all embedded entity columns will be stored in the owning entity table without any prefix. If desired, you can use the #[Embedded( prefix: 'foo' )]
attribute to add a prefix.
#
CollectionsCollections are an ArrayIterator implementation that provides a simple interface to iterate over a collection of entities and add some additional functionality. Collections represent a list of entities. Those are used for *Many relations.
This is a concept that is heavily inspired by Doctrine Collections.
Note that collections should always be initiated in the constructor of the entity.