hasMany
Defining
A hasMany
relationship is a one-to-many
relationship. For instance, a User
may have multiple Posts
.
The first value passed to hasMany
is a WireBox mapping to the related entity.
Quick determines the foreign key of the relationship based on the entity name and key values. In this case, the Post
entity is assumed to have a userId
foreign key. You can override this by passing a foreign key in as the second argument:
The inverse of hasMany
is also belongsTo
.
Inserting & Updating
There are two ways to add an entity to a hasMany
relationship. Both mirror the insert API for entities.
save
You can call the save
method on the relationship passing in an entity to relate.
This will add the User
entity's id as a foreign key in the Post
and save the Post
to the database.
Note: the
save
method is called on theposts
relationship, not thegetPosts
collection.
create
Use the create
method to create and save a related entity directly through the relationship.
This example will have the same effect as the previous example.
Removing
Removing a hasMany
relationship is handled in two ways: either by using the dissociate
method on the belongsTo
side of the relationship or by deleting the belongsTo
side of the relationship.
Last updated