tags:

views:

136

answers:

2

Greetings cake experts, I have a question that I think would really help a lot of people getting started with cakePHP. I have a feeling it will be easy for some of you, but it is quite challenging to me.

I have a simple database with multiple tables. I can't figure out how to make cakePHP display the values associated with a foreign key in an index view. Or create a view where the fields of my choice (the ones that make sense to users like location name - not location_id can be updated or viewed on a single page).

I have created an example at http://lovecats.cakeapp.com that illustrate the question. If you look at the page and click the "list cats", you will notice that it shows the location_id field from the locations table. You will also notice that when you click "add cats", you must choose a location_id from the locations table. This is the automagic way that cakePHP builds the app. I want this to be the field location_name.

alt text

The database is setup so that the table cats has a foreign key called location_id that has a relationship to a table called locations.

This is my problem: I want these pages to display the location_name instead of the location_id. If you want to login to the application, you can go to http://cakeapp.com/sqldesigners/sql/lovecats and the password 'password' to look at the db relationships, etc.

How do I have a page that shows the fields that I want? And is it possible to create a page that updates fields from all of the tables at once?

This is the slice of cake that I have been trying to figure out and this would REALLY get me over a hump. You can download the app and sql from the above url.

+1  A: 

Hah, you figured it out correctly! Cake uses the $displayField variable do decide what to..well, display.

If I remember correctly, by default, cake looks for 'title' and 'name' fields, and if those two are not available, it will simply show your primary key field. Luckily, you can override it the way you figured out yourself ;)

dr Hannibal Lecter
Thanks for the validation on this. I will have to play around with the title/name field on the tables to make it work as auto-magically as possible.
XL
+1  A: 

I guess you need to echo $cat['Location']['location_name'] in your view.

bancer