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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:42:51+00:00 2026-06-11T18:42:51+00:00

In a controller/index action in my CakePHP2 app, I have $this->Model-find(‘all’,array( … lots conditions

  • 0

In a controller/index action in my CakePHP2 app, I have

$this->Model-find('all',array( ... lots conditions and things... ));

Then, in controller/view I have:

$this->Model-find('first',array( ... lots conditions and things... ));

Both of these work really well, but I’m obviously repeating a large chunk of code (namely, the find conditions, field list etc) with the only two differences being that in the first one I’m doing find('all') and in the second one I’m doing find('first') and providing the id as one of the conditions.

Is there a way that I can move the conditions into the model and then run different types of find on those conditions?

  • 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-11T18:42:52+00:00Added an answer on June 11, 2026 at 6:42 pm

    You can create a method in Youur Model:

    public function getConditions($id = null) {
       return array( ... lots conditions and things... );
    }
    

    somewhere in this method just test if (!empty($id)) to return correct result. (You haven’t provide the ‘conditions and things’ so I can’t write the exact code here…

    and use it in both controler actions:

    $this->Model->find('first', $this->Model->getConditions()));
    
    $this->Model->find('all', $this->Model->getConditions()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have an Index action for a Users controller that takes an
I have a controller named Content, which has an Index action, which is associated
I need to test a controller, of action index (generated by the grails generate-all
Consider this MapRoute: MapRoute( ResultFormat, {controller}/{action}/{id}.{resultFormat}, new { controller = Home, action = Index,
My Index Controller: class IndexController < ApplicationController def index @posts = Post.find(:all, :limit =>
In my cakephp app I have an Option model. In my option/index view I
map.root :controller => main, :action => index is not redirecting the main controller to
my view: <form action=<?=base_url()?>index.php/frontend/main_con/temp> <input type=text name=temp> <input type=submit/> </form> Controller: function temp(){ echo
I did rails generate controller home index But it adds this line to my
Hi I'm setting up admin routing in CakePHP. This is my current route: Router::connect('/admin/:controller/:action/*',

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.