Wednesday, April 1, 2009

Re: Accessing Model From another Controller

There are a couple of ways to do this. Besides using an
element/requestAction, you can grab your data from within the
controller action like so:

$data = ClassRegistry::init('YourModel')->find(...)

or

App::import('Model', 'YourModel');
$YourModel = new YourModel();

$data = $YourModel->find(...)

The former would be better if all you want is to grab some data. Use
App:import if you need to actually save something, etc.

On Wed, Apr 1, 2009 at 4:41 PM, jaesun <jasonrider.lv@gmail.com> wrote:
>
> I'm pretty new to the MVC paradigm in general, and just started with
> this project in CakePHP at my job.
>
> I am trying to figure out how to access a model from another
> controller.
>
> From what I have read, normally, you can do that by setting the
> Associations (belongsTo, hasMany, etc) in the Models, and then in the
> EventsController, $this->Car->find('all') etc.
>
> But it looks like with those relationships, you ahve to setup keys
> between each of the tables so that it can join the models together
> correctly.
>
> but what if i want to get access from another model that has no
> association with the current controller that I am in?  There is no
> (user hasMany recipes, or recipes belongsTo user, etc) relationships.
> There is no relation between the tables.  But I would like to display
> the data from that table/model into this controller/view.
>
> I am not sure if I am missing something really simple, though I am
> sure that I am (sorry for the noob question).
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: