One thing with which I have long had problems, within the CakePHP framework, is defining simultaneous hasOne and hasMany relationships between two models. For example:
BlogEntry hasMany Comment
BlogEntry hasOne MostRecentComment (where MostRecentComment is the Comment with the most recent created field)
Defining these relationships in the BlogEntry model properties is problematic. CakePHP’s ORM implements a has-one relationship as an INNER JOIN, so as soon as there is more than one Comment, BlogEntry::find('all') calls return multiple results per BlogEntry.
I’ve worked around these situations in the past in a few ways:
-
Using a model callback (or, sometimes, even in the controller or view!), I’ve simulated a MostRecentComment with:
$this->data['MostRecentComment'] = $this->data['Comment'][0];
This gets ugly fast if, say, I need to order the Comments any way other than byComment.created. It also doesn’t Cake’s in-built pagination features to sort by MostRecentComment fields (e.g. sort BlogEntry results reverse-chronologically byMostRecentComment.created. -
Maintaining an additional foreign key,
BlogEntry.most_recent_comment_id. This is annoying to maintain, and breaks Cake’s ORM: the implication isBlogEntry belongsTo MostRecentComment. It works, but just looks…wrong.
These solutions left much to be desired, so I sat down with this problem the other day, and worked on a better solution. I’ve posted my eventual solution below, but I’d be thrilled (and maybe just a little mortified) to discover there is some mind-blowingly simple solution that has escaped me this whole time. Or any other solution that meets my criteria:
- it must be able to sort by MostRecentComment fields at the
Model::findlevel (ie. not just a massage of the results); - it shouldn’t require additional fields in the
commentsorblog_entriestables; - it should respect the ‘spirit’ of the CakePHP ORM.
(I’m also not sure the title of this question is as concise/informative as it could be.)
The solution I developed is the following:
The notion is simple. The
_bindMostRecentCommentmethod defines a fairly standard has-one association, using a sub-query in the association conditions to ensure only the most-recent Comment is joined to BlogEntry queries. The method itself is invoked just before anyModel::find()calls, the MostRecentComment of each BlogEntry can be filtered or sorted against.I realise it’s possible to define this association in the
hasOneclass member, but I’d have to write a bunch of raw SQL, which gives me pause.I’d have preferred to call
_bindMostRecentCommentfrom the BlogEntry’s constructor, but the Model::bindModel() param that (per the documentation) makes a binding permanent doesn’t appear to work, so the binding has to be done in the beforeFind callback.