Skip to content

Instantly share code, notes, and snippets.

@ADmad
Created January 8, 2016 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ADmad/0a451d837f019edf3447 to your computer and use it in GitHub Desktop.
Save ADmad/0a451d837f019edf3447 to your computer and use it in GitHub Desktop.
<?php
// Posts hasMany Images
// Posts hasOne FeaturedImages: where featured image is the first image for that post.
$this->hasOne('FeaturedImages', [
'className' => 'Images',
'foreignKey' => false,
'conditions' => function ($exp, $query) {
$subquery = $query
->connection()
->newQuery()
->select(['SubFeaturedImages.id'])
->from(['SubFeaturedImages' => 'images'])
->where(['SubFeaturedImages.post_id = Posts.id'])
->order(['SubFeaturedImages.id' => 'ASC'])
->limit(1);
return $exp->add(['FeaturedImages.id' => $subquery]);
}
]);