tags:

views:

67

answers:

2

I have a form which looks like this:

                      Delete
    [Publisher One  ] []
    [Publisher Two  ] []
    [Publisher Three] []
 Add[               ]

So basically, every Publisher appears on the page in its own field. I can modify any of the Publisher names, delete any of the Publishers, or add a new publisher, all on one form, simply by saveAll-ing the form. I know that this will not hold up under 10,000 rows, but I am using CakePHP to remake an existing tool, and I am sure there will be a manageable number of rows.

The problem is that on first load, I have to pre-populate the fields. Now, from the CakePHP book, I am supposed to create the form with Model.n.field. However, the data I pull using a $this->Model->find('all') is in $data[n][Model][field] form. Am I going to have to mangle the data myself to get it in $data[Model][n][field] form, or is there an easy way to do that from within the find command, or perhaps a helper function to turn it from one into the other?

A: 

The difficulties in moving data in different forms around in Cake isn't the smoothest, I agree. But why don't you just do this:

  • pull your data in the controller. $data is an array with the nth record as the first index; then Modelname, then fieldname (sorry for the confusing wording -- this is just how you've done it above).

in your view:

 foreach( $data as $publisher-id => $record )
 {
   echo '<tr><td>' . $record['Model']['publisher_name'] . '</td>';
   echo '<td>' . $form->input( 'Model.publisher_id', array( 'value' => $model['Modelname']['publisher_id']) ) . '</td></tr>';
 }

So I don't really think you need to mangle the data, or the arrays returned from the find operation. Try to just think about how you're going to output it, and you have to be a little clever about it.

Travis Leleu
+1  A: 

The Set class might be able to do the reprocessing for you - see CakeBook and an example. Also other Set class methods might be useful for you.

michaelc