Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
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 belongsTo
method specifying your parent table's custom key.
The inverse of hasOne
is belongsTo
. 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.
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
.
Name
Type
Required
Default
Description
relationName
string
true
The WireBox mapping for the related entity.
foreignKey
String | [String]
false
entityName() & keyNames()
The foreign key on the parent entity.
localKey
String | [String]
false
keyNames()
The local primary key on the parent entity.
relationMethodName
String
false
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.
DO NOT PASS A VALUE HERE UNLESS YOU KNOW WHAT YOU ARE DOING.
Returns a HasOne relationship between this entity and the entity defined by relationName
.
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 .
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 or .
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
.
A hasManyThrough
relationship is either a one-to-many
or a many-to-many
relationship. It is used when you want to access related entities through one or more intermediate entities. The most common example for this is through a pivot table. For instance, a User
may have multiple Permissions
via a UserPermission
entity. This allows you to store additional data on the UserPermission
entity, like a createdDate
.
The only value needed for hasManyThrough
is an array of relationship function names to walk through to get to the related entity. The first relationship function name in the array must exist on the current entity. Each subsequent function name must exist on the related entity of the previous relationship result. For our previous example, userPermissions
must be a relationship function on User
. permission
must then be a relationship function on the related entity resulting from calling User.userPermissions()
. This returns a hasMany
relationship where the related entity is UserPermission
. So, UserPermission
must have a permission
relationship function. That is the end of the relationship function names array, so the related entity resulting from calling UserPermission.permission()
is our final entity which is Permission
.
Let's take a look at another example. HasManyThrough
relationships can go up and down the relationship chain. Here's an example of finding a User's teammates
The inverse of hasManyThrough
is either a belongsToThrough
or a hasManyThrough
relationship.
This approach can scale to as many related entities as you need. For instance, let's expand the previous example to include an Office that houses many Teams.
This next example can get a little gnarly - you can include other hasManyThrough
relationships in a hasManyThrough
relationship function names array. You can rewrite the officemates
relationship like so:
As you can see, this is a very powerful relationship type that can save you many unnecessary queries to get the data you need.