Skip to content

annotation Athena::ORM::Annotations::ManyToOne #

Maps an instance variable to a single entity, where many entities may be associated with the same one. It's always the owning side of the association, holding the foreign key column.

@[AORMA::Entity]
class User < AORM::Entity
  # ...

  @[AORMA::ManyToOne]
  property address : Address? = nil
end

The target entity is inferred from the instance variable's type. Here the foreign key column is address_id, referencing the id of the address; use AORMA::JoinColumn to name the columns differently. The target entity can also map the association, as a AORMA::OneToMany collection naming this property via mapped_by.

The target 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.

Configuration#

Optional Arguments#

inversed_by#

Type: String? Default: nil

The name of the AORMA::OneToMany 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".


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 association is loaded lazily when typed as AORM::Proxy(T)?, as described above.