Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:43:02+00:00 2026-05-30T21:43:02+00:00

In nearly every model, I end up writing code similar to the below example

  • 0

In nearly every model, I end up writing code similar to the below example code. It checks to see if options like limit, order, conditions…etc are sent and changes the query based on that.

Just seems like there MUST be a better way, since most of this stuff is duplicated over and over in many models. Maybe I can use a behavior? Or maybe something else I’m completely overlooking?

I feel like I’ve tried to reinvent the wheel as far as the model code is concerned, but I’d really like to know what the wheel IS – ie what do most people do to manage their model code? I assume it’s “normal” for models to be similar like this? Looking for best practice for this overall “similar model code” concept.

//Restaurant model
function getRestaurants($opts = null) {

    //initialize
    $findType = 'all';
    $params['conditions'] = array();    

    //order
    $params['order'] = 'Restaurant.name ASC';
    if(!empty($opts['order'])) $params['order'] = $opts['order'];

    //limit
    if(!empty($opts['limit'])) {
        $params['limit'] = $opts['limit'];
        if($params['limit'] == 1) $findType = 'first';
    }

    /*... ETC ETC
    - can pass conditions, pricing, days open....etc
    - obviously some things are only for this model, but things like
      limit, order, conditions...etc are for all my models
    */

    //pagination
    $paginate = false;
    if(isset($opts['paginate'])) {
        if($opts['paginate']) {
            $paginate = true;
        }
    }

    //either return the paginate options just created
    if($paginate) {
        return $params;

    //or return the restaurant data found
    } else {
        $data = $this->find($findType, $params);
        return $data;
    }
}

In some, there are more complicated things like – whether or not to contain/join certain models based on an option sent…etc etc.

I’m trying to stick with the MVC concept and keep all my DB stuff in the models.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:43:03+00:00Added an answer on May 30, 2026 at 9:43 pm

    If the code is the same for all models you could use a behavior and simply attach it to the models and change the query using the beforeFind() callback.

    Putting the code into AppModel is also fine and calling it inside the beforeFilter() of the models that need it. But I think the behavior is less work to type. 😉 So I would go for the behavior. If you need more fanciness like specific options for a model while having a set of defaults you could simply change the behavior to support that by merging options from a property of the model with the defaults in the behavior. If you would be more specific i could provide a better solution.

    And finally: There is no common or 100% right way to do it. I would always choose the solution that stays close to MVC and follows KIS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a user-management plugin for grails? Nearly every website requires things like: Users
I've got some code that runs on every (nearly) every admin request but doesn't
I recently inherited some C# code where nearly every item in a file was
I hate seeing nearly every directory in my repository list each file twice, once
I've nearly finished the development of a project and would like to test its
I have looked for an answer for this nearly every where that I can
I have an existing site (where nearly every line has been coded by hand)
Lately, I've been adding asserts to nearly every single function I make to validate
I'm working on a command line application for ultrasound simulation in MATLAB. Nearly every
I notice that I import many of the same imports into nearly every view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.