0

I have 3 tables.

Table1 has a one-to-many relationship to Table2.

Table2 has a one-to-many relationship to Table3.

I would like to write a web service API that allows the user to add rows to all 3 tables at the same time. The problem with CakePHP is that it only allows a controller to access its own corresponding model's data.

How can a controller access other models?

For my case in particular, which table should I use? Will Table1 be a good start since it is the parent to the other tables?

I am using Cakephp2.4.5

3 Answers 3

2

You need to load model try this

$tempfirst = array();
$this->loadModel('Model1');
$tempfirst = $this->Model1->find();



$tempsecond = array();
$this->loadModel('Model2');
$tempsecond = $this->Model2->find();

OR

you can use "uses" property of controller class

public $uses = array('Model1', 'Model2');
Sign up to request clarification or add additional context in comments.

1 Comment

Is this method $this->Model1->Model2->method() advisable in my case? I saw it in another thread. stackoverflow.com/questions/2821499/…
2

I will suggest to go with associations. Cakephp has the great facility of the association. As we can see your model are associated. "loadModel" is used in case when it is not associated with other model which we need to fetch or insert.

In your case you can go with any model because rest are associated with another to which one has association.

i.g Table1 <=> Table2 Table2 => Table3

Table1 <=> Table2 =>Table3 (this chain has created). you can access as well insert data.

To save data in associated model, refer this link.

http://book.cakephp.org/2.0/en/models/saving-your-data.html

Comments

0

if you like to add rows at a same time, you may use $this->ModelName->save associated().

well, i'm not sure.

Comments

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.