How to make cakePHP retrieve data represented by a foreign key?

I have a simple database with multiple tables. I can't figure out how to make cakePHP display the values ​​associated with the foreign key in the index view. Or create a view in which the fields of your choice (those that make sense to users, such as the location name, not location_id, can be updated or viewed on the same page).

I created an example at http://lovecats.cakeapp.com that illustrates the question. If you look at the page and click on the “cats list”, you will notice that it displays the location_id field from the location table. You will also notice that when you click "add cats", you must select location_id from the location table. This is an automated way to create a cakePHP application. I want this to be the location_name field.

alt text

The database is configured so that table cats have a foreign key called location_id, which is related to a table called locations.

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

How do I have a page that displays the fields I want? Is it possible to create a page that simultaneously updates fields from all tables?

This is a piece of cake that I was trying to figure out, and it REALLY will return me for the hump. You can download the application and sql from the above url.

+4
source share
2 answers

Yes, you got it right! Cake uses the $ displayField variable to decide what to do, display.

If I remember correctly, by default, the cake searches for the "name" and "name" fields, and if these two are not available, it will simply show your main key field. Fortunately, you can redefine it as you yourself understood;)

+3
source

I think you need to display $ cat ['Location'] ['location_name'] in your view.

+1
source

Source: https://habr.com/ru/post/1305318/


All Articles