tags:

views:

29

answers:

2

Hi all,

Say I got two Controllers like this
Table1sController, and Table2sController.
with corresponding Models:
Table1sModel, Table2sModel

In the Table1sController, I got this:
$this->Table1sModel->action();

Say I want to access some data in Table2sModel
How is it possible to do something like this in Table1sController
I have tried this in Table1sController:
$this->Table2sModel->action();
But I received an error message like this:
Undefined property: Table1sController::$Table2sModel

+3  A: 

There are a few ways to go here.

If your models have defined associations (hasMany, etc.), then you can access that model's methods (assuming you're in Model1Controller) with:

$this->Model1->Model2->method();

If there is no model association between the two models, but you want to be able to use the Model2's methods, you can add an entry in the $uses attribute of model1Controller. See http://book.cakephp.org/view/53/components-helpers-and-uses

Finally, if it's a transitory connection (you don't want the overhead of loading other models every time, because you're only rarely going to access model2), check out the manual's section on creating / destroying associations on the fly, at http://book.cakephp.org/view/78/Associations-Linking-Models-Together

Travis Leleu
+1  A: 

Something is inherently wrong with what you are doing.

In any controller, you can specify $uses = array('Table1sModel', 'Table2sModel', 'LolModel') and use each Model you need in your controller. You are not calling another controller to access a Model. Models are for data access, you access the needed ones directly from any controller.

I understand, that many MVC examples are almost always show you one page of one controller with one model which is horribly wrong as 99% of the cases you have one site from one controller using many different parts of different models.

(If you really need to call an action, use $this-requestAction())

sibidiba