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

2014-01-20 Thread Борислав Събев
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+unsubscr...@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.


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

2014-01-19 Thread Sam
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+unsubscr...@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.


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

2014-01-19 Thread Reuben
Generally, it's bad practice for a View to perform direct finds against a 
Model.  Even when the model is related to a view via convention, it is 
usually the controller that has performed the find, and executed a 
$this-set(compact('modelData')) or similar to expose the model data to a 
view.

With that in mind, you can expose any model to the view via the controller, 
by finding the data in the controller, and exposing via $this-set().  You 
just may need to the unrelated models in the controller via 
$this-loadModel(), or in the $uses variable of the controller, if it is 
used often.

In the View or in Elements, you may be able to use $this-requestAction() 
to call a specific controller action to get data to display in a View or 
Element, however, this is not something I do, so someone else or the 
CakePHP Book may have better guidelines on actual usage.

Regards
Reuben Helms

On Monday, 20 January 2014 12:32:05 UTC+10, 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+unsubscr...@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.