How to give empty conditions in controller of cakephp 3.2 during model bind?

98 views Asked by At

I am new to cakephp 3. Below is my code.

  $getFlags = $this->Posts->find('all')->contain(['Users', 'Flags.Users', 'Flags.FlagReasons', 'Flags.Users.Schools']);

output

{
        "id": 114,
        "title": "",
         "allow_comment": 0,
        "owner_id": 84,
        "created": "2016-04-08T04:41:08+0000",
        "status": 1,
        "flags": [],
        "user": {
            "id": 84,
             "profile_image": "bc7484cd8caad0de055f8d7ef15551f5.png",
            "is_active": 1
        }

Here i want to give a condition,when there is an empty flag ,it will not be listed in result. Only post having flag will come. Please suggest me. ANy suggstion will highly appreciate.

1

There are 1 answers

0
floriank On

There are two ways to get this done:

  • Use a counter cache and filter by that field (flag_count > 0)
  • Do a subquery to get the count (inefficient)