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 9309953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:59:44+00:00 2026-06-19T00:59:44+00:00

I often create small functions in my Models and Behaviors that add extra conditions

  • 0

I often create small functions in my Models and Behaviors that add extra conditions to the array of a find operation in CakePHP, and I keep thinking of three possible was of implementing those functions. It seems to always come down to three possible code patterns.

I’ll demonstrate these three patterns using the a function called limitErrorCount that will add an extra rule for find queries.

Pattern #1: Argument and Return

public function limitErrorCount(array $conditions)
{
    $conditions['AND'][] = 'Document.errors <'=>10;
    return $conditions;
}

$conditions = array(....);
$conditions = limitErrorCount($conditions);
$records = $this->find('all',$conditions);

Pattern #2: Pass By Reference

public function limitErrorCount(array &$conditions)
{
    $conditions['AND'][] = 'Document.errors <'=>10;
    return $conditions;
}

$conditions = array(....);
limitErrorCount($conditions);
$records = $this->find('all',$conditions);

Pattern #3: Return and Merge

public function limitErrorCount()
{
    $conditions = array('AND'=>array('Document.errors <'=>10));
    return $conditions;
}

$conditions = array(....);
$conditions = Hash::merge($conditions,limitErrorCount());
$records = $this->find('all',array('conditions'=>$conditions));

All three patterns work.

I am wondering which method is best out of the three, and maybe some expert PHP insight as to why it’s better.

  • 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-06-19T00:59:45+00:00Added an answer on June 19, 2026 at 12:59 am

    nice idea, I would like to see implement it in a chain-able way like in jQuery

    $cnd = $this->Document->filterFreshOnes()->filterStarred()->filterByDaysAge('3');
    $res = $this->Document->find('all', array('conditions'=>$cnd);
    

    but that will require holding it in the model variable, which may backfire in cases where you forgot there is already some filter already… ;(

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

Sidebar

Related Questions

When creating WPF user controls I often find myself creating small converter classes when
When I'm developing ASP.NET applications I often create forms that allow users to create,
I am learning C# AND Java and I often create small projects and programs
I often find myself writing small (5-20 lines) files for things like input validation,
In the many Python applications I've created, I often create simple modules containing nothing
Often I create Child threads within the main() as Thread thread = new Thread(new
I often like to create backups when testing the software I work on, and
Very often, I just create a new folder and test things out in there.
I was wondering, when you create an object you can often set multiple attributes
Based on constructor of the class. create or destroy new methods. I often requires

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.