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 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

4 Comments

That worked like a charm! I was also playing around a bit with adding the paginate methods I had in the model as well, but a)I'm reading that paginating in the model is bad 2)I couldn't get it to work anyways 3)it hinges on what 'type' the Unit is (tho I thought that I could pass a variable called $type. So, for now, I am leaving all the code in my controller, even tho the vast majority of the bits are repeated.
Yeah, you can move the query to the model to slim the controller, though. $this->Paginator->settings = $this->Unit->getQuery();, then get the results, $r = $this->Paginator->paginate($this->Unit);
Is this for paginating results or the Paginator helper for displaying links to the paginated results? And does this code go into the controller? Plus, when I tried to move the paginate code into the model I wasn't sure that I had the syntax correct.
That is controller code to get results. The way you're doing it is deprecated. It's just a little cleaner to put the array in the model. From above, that getQuery() function would go in the Unit model and all it would do is return the array that you have in your example as $this->paginate['Unit']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.