annotation Athena::ORM::Annotations::OneToOne
#
Maps an instance variable to a single entity, where each entity is associated with at most one entity on the other side.
@[AORMA::Entity]
class Product < AORM::Entity
# ...
@[AORMA::OneToOne]
property shipment : Shipment? = nil
end
The target entity is inferred from the instance variable's type.
The entity holding the foreign key column is the owning side of the association.
Here that's Product, via a shipment_id column referencing the id of the shipment; use AORMA::JoinColumn to name the columns differently.
Bidirectional#
Mapping the association on the target entity too allows navigating it from both sides. That side, the inverse side, names the owning side's property via mapped_by, and the owning side names the inverse side's property via inversed_by:
@[AORMA::Entity]
class Customer < AORM::Entity
# The inverse side.
@[AORMA::OneToOne(mapped_by: "customer")]
property cart : Cart? = nil
end
@[AORMA::Entity]
class Cart < AORM::Entity
# The owning side, holding the `customer_id` column.
@[AORMA::OneToOne(inversed_by: "cart")]
property customer : Customer? = nil
end
Only the owning side is checked for changes when flushing, see the associations section of the manual.
Loading#
The target of the owning side is loaded along with the entity.
Any target that isn't in the identity map already is loaded with its own SELECT.
Typing the instance variable as AORM::Proxy(T)? instead loads the target only once it's used, see AORM::Proxy.
The inverse side is always loaded along with the entity.
Configuration#
Optional Arguments#
mapped_by#
Type: String? Default: nil
The name of the owning side's property on the target entity. Makes this property the inverse side of the association.
inversed_by#
Type: String? Default: nil
The name of the inverse side's property on the target entity, if the association is bidirectional.
cascade#
Type: Array(String)? Default: nil
The operations on this entity that are also applied to the associated entity, see the associations section of the manual.
One or more of "persist", "remove", "detach", or "all".
orphan_removal#
Type: Bool Default: false
Whether the associated entity is removed when it's replaced by another entity or set to nil.
On the owning side, it also implies the "remove" cascade.
target_entity#
Type: AORM::Entity.class? Default: the instance variable's type
The class of the associated entity, if it can't be inferred from the instance variable's type.
fetch_mode#
Type: AORM::Mapping::FetchMode Default: :lazy
When the associated entity is loaded.
Todo
Not supported yet; it's ignored.
The owning side is loaded lazily when typed as AORM::Proxy(T)?, as described above.