Named finder methods

Published on and tagged with behavior  cakephp

Recently, I stumbled upon a new Rails feature called named_scope.

Inspired by this feature I wrote — just for fun — a small behavior which allows you to define named finder methods (download).

Let’s say we want to define a named finder method to get the most recent posts. The code for it is quite straightforward (you can use the same options as for the built-in find method):

// in the Post model
public $actsAs = array('NamedFinder' => array('recent' => array('order' => 'Post.created DESC', 'limit' => 10)));

With this definition in place, you can then use the named finder method in the following way:

// in your controller (e.g. PostsController)
$this->Post->findRecent();

You can also combine them with “And” if you defined multiple named finders:

$this->Post->findRecentAndPublished();

I’m not sure if this behavior is really useful in practice, as it is much less powerful than the named_scope feature of Rails, and you can accomplish (almost) the same with custom model methods or by defining a custom find type

10 comments baked

  • Abhimanyu Grover

    Excellent man!!!… just what I need. Adding it to my CMS project coming Monday. I liked the support of ‘And’… Nice work.

  • links for 2008-07-07 « Richard@Home

    […] Named finder methods – cakebaker (tags: cakephp behavior finder) […]

  • Brandon P

    I worry that concepts like this lead programmers to be more sloppy and lazy. It doesn’t make sense to me to create MORE of these “magic” functions that hide real functionality.

    IMO, this concept just adds another layer of code that needs to be maintained and executed at runtime to essentially “mimic” a hardcoded function. It’s like you are trying to abstract code that’s already abstract. :)

    It would take you LESS time to simply define a function and return a $this->find() statement. Maintaining code that is mixed with hardcoded methods and these “magic” ones would be a nightmare!

    I think a quick pro/con list of using this behavior would help programmers make a more educated decision whether or not to include this type of behavior.

  • Abhimanyu

    @Brandon: If you are working on a project, which acts as a base to all of your projects, I dont see a negative effect of following this way. Infact, this solution provides re-usability and saves you from writing additional 2-3 lines everytime.

    $this->User->findPending(); is much better than those of magic constant statements like these:
    $this->User->findAll(array(‘User.status’=>0));

    Basically this is your toolset, its upto you how you use it. You can make things simpler and complex at both times.

  • Brandon P

    @Abhimanyu:

    I suggested writing an actual function. I think it’s bad to have this long “config” style implementation to hide functionality:

    function findPending()
    {
    return $this->find(‘all’, array(‘conditions’ => array(‘User.is_pending’ => ‘1’)));
    }

    What’s so bad about that? :) It takes LESS time to write (you don’t have to include the behavior). Plus, this way the function is listed in any API and can be indexed by your IDE for quick variable reference. Otherwise, there is no way to know that such a function exists. And believe me, if you have a large project you will forget the function is there if it’s not concretely defined.

  • cakebaker

    @Abhimanyu, Brandon: Thanks for your comments!

    @Abhimanyu: Thanks, I’m glad it is useful for you!

    @Brandon: I agree with you that you have to be careful with using such concepts. And as you can see from the article, I’m not fully convinced it’s a good idea to use the behavior I wrote ;-)

    The scenario for which I think it could be useful is when you have conditions you want to use in different combinations. But even then you could encapsulate those conditions in private methods and merge them when they are used.

    So, in general it is probably better NOT to use this behavior ;-)

  • speedmax

    same here recently enjoying rails’ named_scope…

    Well, its possible for such NamedFinder behavior in CakePHP, i may even check it out when i have time..

    However, i think the killer feature in rails one is the way ActiveRecord maintained scope can pass over..
    ie, 3 named scope finder chained.
    Product.recent.cheap.downloadable
    The video on railscast talks about the named_scope thingy..

    Anyway, Its very cool dho … such a easy way to declare finder now..

    may be one day, it’s possible to port ActiveRecord::base.with_scope over to cake

  • cakebaker

    @speedmax: Thanks for your comment! Yes, the chaining of scope finders is quite cool and elegant.

  • iamjhlee

    This is useful to make something more organized for me.
    If I need to change something for a general condition, I can change in $actAs, and also this will helps me easily on a big application,

    Thanks, cakebaker

  • cakebaker

    @iamjhlee: I’m glad it is useful for you. I hope you also read Brandon’s comment about the possible problems of using this approach.

Bake a comment




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

© daniel hofstetter. Licensed under a Creative Commons License