Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Relationships are the heart of any ORM engine. They let you interact with relational database tables in an object-oriented way.
Quick's relationship engine provides readable relationship types, extendible relations at runtime, eager loading, and much more.
Start by checking out the different relationship types.
Relationship Types
A hasOne
relationship is a "one-to-one" relationship. For instance, a User
entity might have an UserProfile
entity attached to it.
The first value passed to hasOne
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 UserProfile
entity is assumed to have a userId
foreign key. You can override this by passing a foreign key in as the second argument:
If your parent entity does not use id
as its primary key, or you wish to join the child entity to a different column, you may pass a third argument to the hasOne
method specifying your parent table's custom key.
HasOne
relationships can be configured to return a default entity if no entity is found. This is done by calling withDefault
on the relationship object.
Called this way will return a new unloaded entity with no data. You can also specify any default attributes data by passing in a struct of data to withDefault
.
Returns a HasOne relationship between this entity and the entity defined by relationName
.
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:
If your parent entity does not use id
as its primary key, or you wish to join the child entity to a different column, you may pass a third argument to the hasMany
method specifying your parent table's custom key.
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.
You can also add many entities in a hasMany
relationship by calling saveMany
. This method takes an array of key values or entities and will associate each of them with the base entity.
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.
You can also influence the associated entities by calling "set" & relationshipName
and passing in an array of entities or key values.
After running this code, this user would only have two posts, the posts with ids 2
and 4
. Any other posts would now be disassociated with this user. Likely your database will be guarding against creating these orphan records. Admittedly, this method is not as likely to be used as the others, but it does exist if it solves your use case.
Returns a HasMany relationship between this entity and the entity defined by relationName
.
The inverse of hasOne
is . It is important to choose the right relationship for your database structure. hasOne
assumes that the related model has the foreign key for the relationship.
The inverse of hasMany
is .
There are two ways to add an entity to a hasMany
relationship. Both mirror the for entities.
Removing a hasMany
relationship is handled in two ways: either by using the dissociate
method on the side of the relationship or by deleting the side of the relationship.
Name | Type | Required | Default | Description |
---|
Name | Type | Required | Default | Description |
relationName | string |
| The WireBox mapping for the related entity. |
foreignKey | String | [String] |
|
| The foreign key on the parent entity. |
localKey | String | [String] |
|
| The local primary key on the parent entity. |
relationMethodName | String |
| The method name called on the entity to produce this relationship. | The method name called to retrieve this relationship. Uses a stack backtrace to determine by default. <b></b> DO NOT PASS A VALUE HERE UNLESS YOU KNOW WHAT YOU ARE DOING. |
relationName | string |
| The WireBox mapping for the related entity. |
foreignKey | String | [String] |
|
| The foreign key on the parent entity. |
localKey | String | [String] |
|
| The local primary key on the parent entity. |
relationMethodName | String |
| The method name called on the entity to produce this relationship. | The method name called to retrieve this relationship. Uses a stack backtrace to determine by default. <b></b> DO NOT PASS A VALUE HERE UNLESS YOU KNOW WHAT YOU ARE DOING. |
A belongsTo
relationship is a many-to-one
relationship. For instance, a Post
may belong to a User
.
The first value passed to belongsTo
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:
If your parent entity does not use id
as its primary key, or you wish to join the child entity to a different column, you may pass a third argument to the belongsTo
method specifying your parent table's custom key.
The inverse of belongsTo
is hasMany
or hasOne
.
To update a belongsTo
relationship, use the associate
method. associate
takes the entity to associate as the only argument.
associate
does not automatically save the entity. Make sure to call save
when you are ready to persist your changes to the database.
To remove a belongsTo
relationship, use the dissociate
method.
dissociate
does not automatically save the entity. Make sure to call save
when you are ready to persist your changes to the database.
You can also influence the associated entities by calling "set" & relationshipName
and passing in an entity or key value.
After executing this code, the post would be updated in the database to be associated with the user with an id of 1
.
BelongsTo
relationships can be configured to return a default entity if no entity is found. This is done by calling withDefault
on the relationship object.
Called this way will return a new unloaded entity with no data. You can also specify any default attributes data by passing in a struct of data to withDefault
.
Returns a BelongsTo relationship between this entity and the entity defined by relationName
.