Reputation: 2434
not sure if this is the best way to do what I need but this is my issue. In my edit function view I recreate a forms fields based on what's in $this->data->expense
but I need the array to be in a certain order or the fields get generated in the wrong order. This is the array I have:
'Expense' => array(
(int) 0 => array(
'id' => '98',
'date' => '2012-08-23',
'sitename' => '123',
'detail' => 'Breakfast',
'amount' => '100.00',
'miles' => null,
'total' => '100.00',
'expense_claim_id' => '63',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 09:08:52',
'ExpenseClaim' => array(
'id' => '63',
'user_id' => '3',
'claim_status_id' => '1',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 10:14:10',
'approved' => false,
'approved_by' => '0',
'date_submitted' => '2012-08-23 09:08:52'
),
'ExpenseCode' => array(
(int) 0 => array(
'id' => '1',
'name' => 'Plane fare',
'code' => '1',
'created' => '2012-07-31 09:52:02',
'modified' => '2012-07-31 09:53:57'
)
)
),
This is how I need it to be ordered (ExpenseCode
) appears higher up:
'Expense' => array(
(int) 0 => array(
'id' => '98',
'date' => '2012-08-23',
'sitename' => '123',
'detail' => 'Breakfast',
'ExpenseCode' => array(
(int) 0 => array(
'id' => '1',
'name' => 'Plane fare',
'code' => '1',
'created' => '2012-07-31 09:52:02',
'modified' => '2012-07-31 09:53:57'
)
),
'amount' => '100.00',
'miles' => null,
'total' => '100.00',
'expense_claim_id' => '63',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 09:08:52',
'ExpenseClaim' => array(
'id' => '63',
'user_id' => '3',
'claim_status_id' => '1',
'created' => '2012-08-23 09:08:52',
'modified' => '2012-08-23 10:14:10',
'approved' => false,
'approved_by' => '0',
'date_submitted' => '2012-08-23 09:08:52'
)
),
How can I achieve this and will changing the structure of it affect cake when I post?
Upvotes: 0
Views: 404
Reputation: 29121
This makes no sense. If the order of the data in an associative array is causing your HTML fields/form inputs..whatever to be in the wrong order, you're doing it wrong.
Try using the keys instead of just looping through in order. Or read more about associative arrays - there are a lot of fun / easy things to do to use & manipulate their data... but again to reiterate, there is NO need to return the data in a specific order.
Edit:
You could always create an array of keys and repeat through those. Might be a cleaner way of managing the order.
Upvotes: 0
Reputation: 1546
As far as I know, there is no built-in function on CakePHP to custom sort an array. I guess you will have to do this with pure PHP. https://stackoverflow.com/search?q=custom+sort+array+php
Unless you must have the fields created dynamically, I suggest you to add each field yourself.
will changing the structure of it affect cake when I post?
No, if you simply put the fields in a different order this won't affect CakePHP. The data sent via POST
will be in array $this->data['Expense']
. When saving the data with $this->model->save()
, the order does not matter.
Upvotes: 1