I have ArticleLists
which hasMany ArticleListPosts
, which in turn belongsTo Contents
.
I want to retrieve only the entries related to my below conditions, i.e only ArticleLists
, that have ArticleListPosts
, that their Contents
match the %search%
criteria. This works fine, except that it retrieves all ArticleListPosts
, even if even one has Contents
that match the %search%
criteria. But I want only ArticleListPosts
that have relevant Contents
, not all.
My query is below:
$query = $this->ArticleLists
->find()
->contain([
'ArticleListPosts',
'ArticleListPosts.Contents'
])
->innerJoinWith('ArticleListPosts')
->innerJoinWith('ArticleListPosts.Contents')
->where([
'ArticleLists.site' => $site,
'ArticleLists.culture' => $culture,
'ArticleLists.language' => $language,
'OR' => [
['Contents.slug LIKE' => "%$search%"],
['Contents.title LIKE' => "%$search%"]
],
])
->order([
"ArticleLists.slug" => "ASC",
"Contents.slug" => "ASC"
]);