3

I am having a hard time trying to figure out how to get a sub-query working.

Imagine I have:

    $schools
        ->select($this->Schools)
        ->select([
            'pupilcount' => $this->Pupils
                ->find()
                ->select([
                    $this->Pupils->find()->func()->count('*')
                ])
                ->where([
                    'Pupils.school_id' => 'Schools.id', 

                ]),

The problem I am experiencing (I think) is that Schools.id is always 0 and so the count is returned as 0. I can pull out the Pupils join and it shows Pupils there.

I tried changing my code to add a:

->select(['SCID' => 'Schools.id'])

and reference that in the sub-query but doesn't work, it will always return 0 for the pupilcount.

What am I doing wrong here?

ndm
  • 59,784
  • 9
  • 71
  • 110
Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • Check the generated SQL. Unless being an expression object, the right hand side of a condition will always be bound as a parameter, ie you're comparing against a string literal. – ndm May 01 '17 at 20:22
  • Ooh so I must make it an expression? Because yes, I see that in the SQL – Sammaye May 01 '17 at 20:23
  • @ndm I looked at the functions in the Expression object but I am still unsure which one to choose, I cannot see one that just says "don't treat this as an indentifier" – Sammaye May 01 '17 at 20:26

1 Answers1

7

Whenever encountering query problems, check what queries are actually being generated (for example using DebugKit). Unless being an expression object, the right hand side of a condition will always be bound as a parameter, ie you're comparing against a string literal:

Pupils.school_id = 'Schools.id'

Generally for proper auto quoting compatibility, column names should be identifier expressions. While the left hand side will automatically be handled properly, the right hand side would require to be handled manually.

In your specific case you could easily utilize QueryExpression::equalFields(), which is ment for exactly what you're trying to do, comparing fields/columns:

->where(function (\Cake\Database\Expression\QueryExpression $exp, \Cake\ORM\Query $query) {
    return $exp->equalFields('Pupils.school_id', 'Schools.id');
})

It's also possible to create identifier expressions manually by simply instantiating them:

->where([
    'Pupils.school_id' => new \Cake\Database\Expression\IdentifierExpression('Schools.id')
])

or as of CakePHP 3.6 via the Query::identifier() method:

->where([
    'Pupils.school_id' => $query->identifier('Schools.id')
])

And finally you could also always pass a single string value, which is basically inserted into the query as raw SQL, however in that case the identifiers will not be subject to automatic identifier quoting:

->where([
    'Pupils.school_id = Schools.id'
])

See also

ndm
  • 59,784
  • 9
  • 71
  • 110
  • Aha perfect, I gotta remember that thanks. I am still a bit of a noob at cake so learning as I go along – Sammaye May 01 '17 at 20:46
  • I look at the cookbook docs but I cannot find `equalFields` how do you find this stuff out? – Sammaye May 01 '17 at 20:50
  • 2
    @Sammaye The cookbook doesn't cover the complete API, but the general concepts and most relevant topics. If you know the general concept of how adding conditions work, you'd stumble over this when inspecting the QueryExpression API (guess it wouldn't hurt if the examples in the Cookbook would use proper type hinting in order to support IDE completion/inspection). If you ment how I personally find out, I'm always studying API docs and source code before using stuff, and In the case of CakePHP I'm also a occassional contributer, so I've studied the source intensively by now. – ndm May 01 '17 at 21:30
  • Awesome thanks, I guess it is an experience thing, I am the same with Yii2 – Sammaye May 01 '17 at 21:44