heshanh
heshanh

Reputation: 387

Multiple controllers with a single model

Heres what im trying to do

Ive got table that holds many different types of data for items. the model name for this is 'Object'

for example :

row 1 : item_type = event
row 2 : item_type = news
row 3 : item_type = booking

i want to write a controller 'event_controller' and use the 'Object' model with it and only deal with item_types of event. and another controller for news (news_controller) and use the same model.

how do i do this on cakephp.

Im coming into cake from codeigniter and in CI we can load any model we want into any controller can i do something similar with cake?

Upvotes: 0

Views: 222

Answers (3)

Brandon Cordell
Brandon Cordell

Reputation: 1318

It's considered poor form to use var $uses in CakePHP 1.3+. It's been replaced by App::import (see below)

Given you have a Users controller with User model, and a Comments controller with Comment model. The accepted patterns are:

Using Associations

This is your best bet if the models can be logically associated.

// models/user.php
Class User extends AppModel {
    public $hasMany = array('Comment');
}

// controllers/users_controller.php
Class UsersController extends AppController {
    function allComments() {
        $this->User->Comment->find('all'); // You can use this across multiple models (e.g. $this->User->Comment->NestedComment->find('all');
    }
}

Instantiating the model object

This will load your model file, add the instance to CakePHP's object map, and returns the instance.

// models/user.php
Class User extends AppModel {}

// models/comment.php
Class Comment extends AppModel {}

// controllers/users_controller.php
Class UsersController extends AppController {
    function allComments() {
        $Comments =& ClassRegistry::init('Comment');

        $Comments->find('all');
    }
}

Using $this->loadModel

Internally this uses ClassRegistry::init, then also adds the model as a property of the controller.

// models/user.php
Class User extends AppModel {}

// models/comment.php
Class Comment extends AppModel {}

// controllers/users_controller.php
Class UsersController extends AppController {
    function allComments() {
        $this->loadModel('Comment');

        $this->Comment->find('all'); // using load model allows you to access it via $this->Model
    }
}

App::import

This is really just the CakePHP way of requiring a file. You'll still need to instantiate the object.

// models/user.php
Class User extends AppModel {}

// models/comment.php
Class Comment extends AppModel {}

// controllers/users_controller.php
App::import('Model', 'Comment');
Class UsersController extends AppController {
    function allComments() {
        $Comment = new Comment();

        $Comment->find('all');
    }
}

I hope this helps.

Edit: If you want to use the model object globally within the controller, you can use any of these patterns I specified within your beforeFilter().

Upvotes: 2

Vinayak Phal
Vinayak Phal

Reputation: 8919

I would like to suggest you to not to $uses statement. Instead of that you can use relations of the models like $this->Model->M0del1->....->someFunc(); if the relation exists.

If the relation between the models dos't exist then simply use $this->loadModel('ModelName'); in the specific function where-ever you need it.

If you use var $uses = array('Object'); it becomes global to the controller and it will load that model for all the actions of the controller despite of whether you require it or not. This will affect your performance.

If you use $this-LoadModel('ModelName'); in a particular function it will only load in that function not in all the actions.

Upvotes: 1

8vius
8vius

Reputation: 5836

You declare your controller and declare the $uses variable on it like this:

var $uses = array('Object');

Upvotes: 0

Related Questions