hey Michael,
to use data from multiple tables your controller has to have access to
each of these models either via the assocations that exist between
models or your uses var in your controller.
e.g. Images and Albums
In my Image Model I have a belongsTo association telling cake that
each of my Images can belong to only one Album.
In my Album Model I have a hasMany association telling cake that each
of my Albums can have many Images.
Now, via my Images controller I can access my Image model using:
$this->Image;
Which allows me to use all of the methods associated with that model,
specifically in this instance save();
$this->Image->save($this->data);
As long as there is an element in my data array with the key Image
then whatever attributes are held within that subsequent array will be
saved to my Image table.
i.e. $this->data should look like:
array (
'Image' => array (
'id' => 1,
'description' => 'My image',
'location' => 'image.jpg'
)
)
All you need to worry about is making sure you're data array is in
this format and Cake will save correctly. You can do this via the view
by using the Cake input field naming conventions (data[Image][id])
which is done for you if you use the HTML helper or the Form helper.
Or, you can do this at the controller level by processing your data in
whatever fashion you like, as long as the end result (before saving)
looks like the above.
Now, I went on a bit there but, to grab data from different tables all
you need to do is use the same methods you would use for your Image
Model with your Album model. You can access your album in your
ImagesController via:
$this->Image->Album->find($conditions,$etc);
Because the association exists you always have access to the
associated models. Therefore, similarly you could do t this from your
AlbumsController:
$this->Album->Image->find($etc);
If you haven't set up your associations, or choose not to for various
reasons you can always use the $uses var within any controller to gain
access to various Models.
In your controller definition:
var $uses = array (
'Image',
'Album'
);
Then you will always be able to do things like (from your controller):
$this->Album->findAll();
$this->Album->save();
$this->Image->find($conditions);
$this->Image->save($this->data);
In that last example, as I mentioned above, as long as your data array
of your controller is in the right format Cake will save your data.
You could call save via both your Image and your Album within the one
controller on the following array:
array (
'Image' => array (
'id' => 1,
'description' => 'My image',
'location' => 'image.jpg',
'album_id' => 3
),
'Album' => array (
'id' => 10,
'description' => 'My album',
)
)
All you need to do is make sure your Array is in this format.
If you need more help have a good look at the manual and try some of
the tutorials. It truly is the only way to master Cake. I love this
framework, and it is well worth it to spend the time getting to the
know the finer points.
Another good starting point is the '21 things I learnt about Cake' blog.
http://www.avatarfinancial.com/pages/cake/
Hope this helped.
Cheers,
freedom
On 25/09/06, michael234 <[EMAIL PROTECTED]> wrote:
>
> Hi,
> I'm having a bit of trouble working out how to bring in and update data
> from different table. Basically I'm trying to do two things: populate a
> field with the id from one table and then insert it into another table
> and bring in data from two tables, edit it and then save it back to the
> respective tables. Basically the same problem.
>
> Not sure what I should be putting in the controllers, your expertise
> would be much appreciated.
>
> Thanks, Michael.
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---