Monday, January 20, 2014

Re: Is it possible for a View to access another unrelated Model's data?

As @Reuben explained: binding Models to views directly is bad and should not be done at all. Even $this->requestAction() is bad in my mind, however there are some cases for it.

Anyway, what you're asking for is possible.However I have the feeling that you're not too sure what MVC is, how it should work and especially how this ties into CakePHP's structure. Read this:
http://book.cakephp.org/2.0/en/getting-started/a-typical-cakephp-request.html
and this:
http://book.cakephp.org/2.0/en/getting-started/cakephp-structure.html

Then the answer to your question is:

A controller can use as many models as you like provided that those are defined in the Controller's $uses property.
At the head of your controller you should have an:

    public $uses = array('ControllerModel','OtherModel');

Now the controller will know which Models to initiate and provide them to you in it. This is so because the Controller doesn't need to load all models in your app - just those that it needs.
Now in your Controller action:

    $myOtherModelData = $this->OtherModel->find('all');
    $this->set('myOtherModelData', $myOtherModelData);

Now in the View your data will be in: $myOtherModelData.

So you actually do not bind the Model to the View - you call the model in the Controller.

On Monday, 20 January 2014 04:32:05 UTC+2, Sam wrote:
From what I understand from cakephp, a view is associated to a model and can only access the model's data. Is it possible for a View to access another unrelated Model's data? What I mean is whether it is possible for a view to access any database table's data, even if it is unrelated?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: