0

I'm using Kohana 3.3 ORM and have setup the rules method for validation. Now I would like to actually create a record to my table. I want to populate all the values to my ORM object by calling $ormtable->values($_POST) but my problem is that not all of the field names in the $_POST array exactly match the column name in the table.

For example, my form has a field with the name "billing_address1" but the corresponding table column is "address1".

Is there some existing method in the ORM that does this already? If not, what is the best way to map these alternate field names?

Chad
  • 1,708
  • 1
  • 25
  • 44
  • 2
    I don't believe there is a built in solution for this. Although you could easily copy `$_POST` to a temp array and manually [change the keys](http://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element/) – kero Oct 17 '13 at 13:37

1 Answers1

1

as @kinakero said simply use temp array

$post_array = $_POST;
$post_array['address1'] = $_POST['billing_address1'];
unset($post_array['billing_address1']);

$ormtable->values($post_array);
Vladimir Dimitrov
  • 1,008
  • 7
  • 21
  • 2
    Thanks for your input. I ended up doing just that except wrapping it in a function that extends the Arr class and just passing two arrays, the source and one with the mapping of keys that needed to be changed. https://gist.github.com/chadsaun/7043210 – Chad Oct 18 '13 at 15:23
  • Nice solution. There is a `'` missing at the end of line 11 and the comment is probably self explanatory – kero Oct 18 '13 at 23:05