Hi joel
HABTM means there's a many-to-many relationship between the models - so
the category data will ba available as a list, not as a single item as
you are attempting.
A quick look tells me that in your view you should be accessing the
Category data as an indexed array. So instead of
<td><?php echo $photo['Category']['name']; ?></td>
you should be doing something like
<td><?php echo $photo['Category'][0]['name']; ?></td>
will give you the name of the FIRST category that the photo belongs
to. To get all the categories you will need to iterate through the
list:
foreach($photo['Category'][0] as $category){
<?php echo $photo['Category'][0]['name']; ?>
}
or something similar.
If you put <?php debug($photo) ?> somewhere into your view, Cake will
print_r() your data in a readable format - it may help you understand
how Cake is structuring your data.
Regards,
-ad
j o e l wrote:
> Hello, All.
>
> I hope you don't mind my posting another HABTM issue here. I've been
> trying to get this to work, and I'm sure I'm just missing something
> basic. I've been a little confused reading the manual, the google
> groups, etc. If you don't mind, I'll just post what I'm trying to do
> and then my dbase structure and code.
>
> I am trying to create a photo album for my personal site. I would like
> users to be able to search for specific categories. I've started out
> with 3 tables: one for photos, one for categories, and one for the
> join. I've created the models and controllers according to what I've
> seen in the manual, but it doesn't work. When I perform findAll() via
> the index view, it does not pull in the associated category (there is
> data in all tables). So, that is the issue. Now, here is the code...
>
>
> TABLES:
>
> * categories: id INT, name VARCHAR, description text, parent_id
> INT, created DATETIME, modified DATETIME
> * photos: id INT, file_name VARCHAR, description text, caption
> text, created DATETIME, modified DATETIME
> * categories_photos: category_id INT, photo_id INT
>
>
> MODELS:
>
> category.php:
>
> class Category extends AppModel
> {
> var $name = 'Category';
> var $hasAndBelongsToMany = array('Photo' =>
> array('className' => 'Photo',
> 'joinTable' => 'categories_photos',
> 'foreignKey' => 'category_id',
> 'associationForeignKey' => 'photo_id',
> 'conditions' => '',
> 'order' => '',
> 'limit' => '',
> 'unique' => '',
> 'finderSql' => '',
> 'deleteQuery' => ''));
> }
>
>
> photo.php:
>
> class Photo extends AppModel
> {
> var $name = 'Photo';
> var $hasAndBelongsToMany = array('Category' =>
> array('className' => 'Category',
> 'joinTable' => 'categories_photos',
> 'foreignKey' => 'photo_id',
> 'associationForeignKey' => 'category_id',
> 'conditions' => '',
> 'order' => '',
> 'limit' => '',
> 'unique' => '',
> 'finderSql' => '',
> 'deleteQuery' => ''));
> }
>
>
> Photo Controller:
>
> Everything works in my category views, so I will just paste a portion
> of my Photo Controller.
>
>
> class PhotosController extends AppController
> {
> var $name = 'Photos';
>
> function index()
> {
> $this->set('data', $this->Photo->findAll());
> }
> ....
>
>
> In my index view for the photo table, I have a table with the
> appropriate association which displays all of the data from the photo
> table just fine. However, nothing from the category table displays.
> I've tried defining it several different ways. Currently, I have this
> in the view...
>
>
> <?php foreach ($data as $photo): ?>
>
> <tr>
> <td><?php echo $photo['Photo']['id']; ?></td>
> <td><?php echo $photo['Photo']['file_name']; ?></td>
> <td><?php echo $photo['Photo']['description']; ?></td>
> <td><?php echo $photo['Photo']['caption']; ?></td>
> <td><?php echo $photo['Category']['name']; ?></td>
>
>
> This gives me an error stating 'name' is an 'undefined index'. I
> obviously do not have a complete understanding of how this is supposed
> to work. Would someone mind kindly explaining this to me?
>
>
> Thanks in advance. I greatly appreciate it.
>
>
> -- j o e l
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---