findCount vs. hasAny

Published on and tagged with cakephp  model

While changing some code to make use of the new Containable behavior I noticed code snippets that looked like:

// in a model method
if ($this->findCount($conditions) > 0) {
    // do something
}

If you look at this example you will realize that what the code tells us is slightly different from what the intention of the code is. The code tells us something like: “if the number of records that meet the conditions is greater than 0 then do something”. But what we really want to express is: “if there are any records that meet the conditions then do something”.

Fortunately, there exists the method Model::hasAny() for this purpose (it is not a new method, but for some reason we almost never used it), and by using this method we can simplify the example from above and make it more expressive:

// in a model method
if ($this->hasAny($conditions)) {
    // do something
}

Sure, it is only a detail, but it helps to make the code a little bit more expressive.

7 comments baked

  • Mike karthauser

    A much better method. Fantastic stuff.

  • Joaquín Windmüller

    Specially if you consider that findCount is deprecated and should use find(‘count’, array(‘conditions’ => $conditions)) instead.

    hasAny is not only meaningful but shorter ;)

    Nice

  • Abhimanyu

    Nice as usual, but both the statements are semantically the same :)

    “if the number of records that meet the conditions is greater than 0 then do something”.

    or

    “if there are any records that meet the conditions then do something”.

    And its often difficult to remember all the function names :p

  • Abba Bryant

    Abhimanyu

    The two statements are *not* semantically the same. find( ‘count’ ) returns an integer, and thus requires you to run it against a boolean conditional to get a true / false.

    The other returns a true / false. They are patently different in return types and thus *cannot* be semantically equal without additional code.

  • bfw

    i do this this way in controllers,

    if ($aData = $this->Model->find($conditions)) {

    // here is used $aData

    }

    this way i do not have to check if there are any records and then get them

  • Michał Szajbe

    I agree, hasAny is more code-reader-friendly.

    It’s worth to note, however, that the difference is only when reading the code, because both methods work the same way – hasAny works by calling find(‘count’).

  • cakebaker

    @all: Thanks for your comments!

    @Joaquin: Yes, that’s an additional advantage.

    @Abhimanyu: Yes, it is sometimes difficult to remember all those method names, and so it helps if you dig into the source from time to time ;-)

    @bfw: Yes, that’s a useful approach if you are not only interested whether there are records but also in the respective records.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License