Answered by Eric Peterson
TLDR: Calling a relationship method returns a relationship component. Preceding that call with get
loads and executes the relationship query.
When you define a relationship you name the function without a get
in front of it. When calling a relationship with get
preceding it, Quick loads the relationship and executes the query. You are returned either a single entity (or null
) or an array of entities.
When you call the relationship function you get back an instance of a Quick Relationship component. This component is configured based on the entity that created it and the attributes configured in the relationship call. You can think of a relationship component as a super-charged query. In fact, you can call other Quick and qb methods on the relationship object. This is one way to restrict the results you get back.
For instance, perhaps you want to retrieve a specific Post by its id. In this case, you want the Post to be found only if it belongs to the User. You could add a constraint to a Post query on the foreign key userId
like so:
Another way to write this is by leveraging existing relationships:
Let's disect this. At first glance it may look like it is just a matter of style and preference. But the power behind the relationship approach is that it encapsulates the logic to define the relationship. Remember that relationships don't have to only define foreign keys. They can define any custom query logic and give a name to it. They can even build on each other:
You see here that we have now named an important concept to our domain - a published post. Let's take this one step further and name the query logic on the Post entity itself.
Quick is powered by qb qb documentation. and can be accessed from within Quick using either the getQB
or retrieveQuery
methods. For your convenience, the qb builder will have already populated the database table and column names for you in the returned qb instance.
getX
uses onMissingMethod to call retrieveAttribute
. This is so you can create your own custom getters.