What is the best way to access another CakePHP model in a controller?

Let's say I have two controllers like Table1sControllerand Table2sController. With appropriate models: Table1sModel, Table2sModel.

In Table1sControllerI got the following:

$this->Table1sModel->action();

Let's say I want to access some data in Table2sModel.

How can one do something like this in Table1sController? I tried this in Table1sController:

$this->Table2sModel->action();

But I got an error like this:

Undefined property: Table1sController :: $ Table2sModel

+3
source share
2 answers

There are several ways.

(hasMany ..), ( , Model1Controller):

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

, Model2, $uses model1Controller. . http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses

, ( , model2), / , http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

+8

- , .

$uses = array ('Table1sModel', 'Table2sModel', 'LolModel') , . . , .

, MVC , , 99% , .

( , $this-requestAction())

+2

Source: https://habr.com/ru/post/1745154/


All Articles