Created
January 8, 2016 14:19
-
-
Save ADmad/0a451d837f019edf3447 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]); | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References:
http://stackoverflow.com/questions/30241975/loading-associated-model-data-in-cakephp3
http://stackoverflow.com/questions/29775766/using-limit-on-contained-model
cakephp/cakephp#14427