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

  • Home
  • SEARCH
  • 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 7181197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:30:24+00:00 2026-05-28T17:30:24+00:00

I have a number of functions in my controller logic and for every one

  • 0

I have a number of functions in my controller logic and for every one I have some logic that pulls data for a “featured” set of units. These units are on several pages. How do I go about making this one piece of logic available to all 4 of the views that need it?

For reference, here is part of my controller logic:

public function index() {
        $this->set('title', 'All accommodations available in and near Gulf Shores, AL');


$this->Unit->Behaviors->attach('Containable');
    $this->Unit->contain(
        array(
         'User'=>array(
            'id'),
         'Location',
         'Complex'=>array('location_id'),
         'Image'=>array(
            'logo','img1'
            )
          )
        );
    $c=$this->Unit->find('all', 
        array(

        'limit'=>3,
        'conditions'=>array(
            'active'=>1,
            'featured'=>1
        )
        )
    );
    $this->set('featured', $c);





$this->paginate['Unit']=array(
        'limit'=>9,
        'order' => 'RAND()',
        'contain'=>array(
                'User'=>array('email'),
                'Complex'=>array('location_id','complex_website'),
                'Location',
                'Image'
                ),
        'conditions'=>array(

                'Unit.active'=>1)   
    );
$data = $this->paginate('Unit');
$this->set('allaccommodations', $data);

    }
    public function houses() {

$this->set('title', 'Home rentals available in Gulf Shores');

$this->Unit->Behaviors->attach('Containable');
    $this->Unit->contain(
        array(
         'User'=>array(
            'id'),
         'Location',
         'Complex'=>array('location_id'),
         'Image'=>array(
            'logo','img1'
            )
          )
        );
    $c=$this->Unit->find('all', 
        array(

        'limit'=>3,
        'conditions'=>array(
            'active'=>1,
            'featured'=>1
        )
        )
    );
    $this->set('featured', $c);






$this->paginate['Unit']=array(
        'limit'=>9,
        'order' => 'RAND()',
        'contain'=>array(
                'User'=>array('email'),
                'Location',
                'Image'
                    ),
        'conditions'=>array(
                'Unit.type'=>array('house', 'rentalco'),
                'Unit.active'=>1)   
    );
$data = $this->paginate('Unit');
$this->set('allhouses', $data);



}
...

I have two other functions that set a ‘featured’ variable that is available to the view. I’m sure there is a much better/more efficient way to do this?

  • 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-28T17:30:25+00:00Added an answer on May 28, 2026 at 5:30 pm

    Yes, the idea is to have all the data interaction in your model and have your controller clear and easy to follow. Fat models, skinny controllers. There are custom finds you can use: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types

    but I prefer to just make a model function because they’re easier to work with in the IDE with code completion and documentation. So for the featured find you keep using you could do something in your Unit model like:

    public function getFeatured($limit) {
        $results = $this->find('all', array(
            'limit' => $limit,
            'conditions' => array(
                'active' => 1,
                'featured' => 1
                )
            )
        );
    
        return $results;
    }
    

    Then in the UnitController:

    $this->set('featured', $this->Unit->getFeatured(3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm testing a C++ class with a number of functions that all have basically
I have now written a number of different functions in Google Apps Script that
I have numerous functions (unknown at design time) that each take a specific number
In my controller I have two functions that pull all records from the db
I have some code which is common to a number of my controller actions
I have some reusable HTML snippets that I want to 'include' in a number
I have a javascript object, which I've added a number of prototyped functions to,
I have a Flash movie which has a number of actionscript 2 functions which
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
I have set up a controller called links, i have the routes to several

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.