CakePHP How do I display all table field names

Go To


New to CakePHP, so be gentle...

I'm trying to loop through a table to display all the read-only fields (labels and values). In my edit function, I get the table

$this->set('invoice', $this->Invoice->read(null, $id));

In my view, I want to loop through the entire table schema and output the field names and values as labels like fieldName: value

Invoice Number: SVC00158
Invoice Date: 03/03/12

There are 37 fields in this table. I would rather not have to manually code for every one. I know to retrieve the input fields like this

echo $this->Form->input('purchaseOrderNumber');

but I can't seem to find a 'read-only' attribute for the input() method. Hope that makes sense.

Thanks for your time.

2012-04-03 21:40
by Mike S.
why displaying inputs (readonly) if they are not supposed to be edited? you could just print out the values in a dd list etc, coudnt' you - mark 2012-04-03 22:20


You could loop through the schema and output it like that:

// assumes $result contains model data
$schema = $this->Model->schema();
foreach ($schema as $field => $attrs) {
  echo $result['Model'][$field];
2012-04-03 22:54
by jeremyharris
Here's the solution I found. Taking me awhile to refresh php skills. foreach ($invoice as $tableName) { foreach ($tableName as $fieldName => $fieldValue) { echo $fieldName; ?>  

- Mike S. 2012-04-04 14:57


just write:

2012-04-03 21:52
by Aziz
nice one, but how to exclude a particular field - w3jimmy 2014-01-04 21:15


If you are a beginner I suggest you look into Cake Bake (it will "bake" all your files from the console). I mean the views, controllers, and models based on you current db schema so that will it for you and even apply some styling to it.

I hope that helps

2012-04-04 02:14
by Marius Talagiu