[Coldbox 6.x] - Quick ORM and Relationships

Hi,

Are there any full examples using Quick ORM? Specifically I am trying see an example of how relationships work in Quick. I saw the docs, but they don’t give the full context as in an example showing a handler and model object working together.

My goal is to understand in my handler, how do I retrieve my Job object and have the Company property also be populated.

My code is working to load jobs, but the company property is always empty when I dump it out despite my database having job records in the job table that are linked to company records in the company table via a Foreign Key (FK).

In my Job.cfc, I have this function loadCompany(), but how does that get called? Do I have to call that explicitly?

Here is my code:

component table="job"  accessors="true" extends="quick.models.BaseEntity"{
   property name="jobId" column="job_id" fieldtype="id" generator="identity";
   property name="jobTitle" column="job_title";

  function loadCompany() {
    return hasOne( "Company" );
  }
}

Here is my Company.cfc

component table="company" persistent="true" extends="quick.models.BaseEntity" {
  property name="companyId" column="company_id" fieldtype="id" generator="identity";
  property name="companyName" column="company_name";

  property name="jobs" fieldtype="one-to-many" cfc="Job" fkcolumn="company_id" type="array";
 }

Any help appreciated…

The best open source example right now is the quick-with-auth template. It doesn’t really go in to relationships, but it will show you using Quick in a handler and view.

The next best resource is in progress - it’s the Quick Workshop coming to CFCasts.com. In that course we build an entire blog engine using Quick.

Now, to answer your relationship question, I tried to answer it in this FAQ post on the Quick docs. Basically, you can call your function to get an instance of the relationship (which is kind of like a pre-configured QueryBuilder) or you can prepend the same method name with get to execute the query the relationship builds and load and return the results.

2 Likes