Contain with conditions on ->get()?

It doesn’t appear this is possible.

I’d like to do a simple single-query get($id) on my entity and include associated records, but only those that meet the specified criteria.

So for example: get an article plus the very last comment posted for it and the name of the last user to edit the article… or get the article, plus only comments posted by a specific user/user type.


This may be a good time to ask: why is get() so different from all the other find methods? (i.e., ‘contain’ is an argument)… and why do I seem to detect an inconsistency of when to use ‘conditions’ vs ‘where’ or ‘fields’ vs ‘select’ in these functions… and finally… the number of utterly different ways to talk to the database with cake is huge and they are sometimes interchangeable and inter-compatible and sometimes not. Super confusing.

get is different, precisely because it returns an entity, not a query. Entities don’t have query methods available on them, because they’re entities, not queries. You can put conditions on the the containment like:

$articlesTable->get($id, [
    'contain' => [
        'Comments' => [
            'queryBuilder' => function(Query $q) {
                // Return only the most recent comment
                return $q->order(['Comments.date_posted' => 'DESC'])->limit(1);

If you don’t like that structure, there’s nothing stopping you from using normal finds, and adding ->where(['' => $id])->first() at the end.

1 Like

Interesting. I simply didn’t think I could the queryBuilder function in a get()

I saw that example in the book and tried it using a ->findById(), as well as a ->find() with ->where() for the id… attempting to include an associated model with conditions, plus one more model associated to the associated model, also with its own conditions. It was failing for me. Then I tried cutting back to see if I could get it on just one association and still failed. So I gave up on it and just iterated through my result set and manually rebuilt it :tired_face:

Happy to help get from where you are to where you should be, but you’ll need to post the code that’s not working, and probably also what is for reference.

I appreciate it!

I’ve just started school back up so I’m going to have to table that particular issue for a while. I have a functional workaround for now.