I'm trying to get copyfrom to work in a PHP application I am developing.
Here is the function attached to my PUT /api/visitor/@id route:
public function update($f3, $params) {
$data = json_decode( $f3->get('BODY'), true );
$visitor_id = $params['id'];
$visitor = new \Models\Visitor();
$visitor->load(array('id = ?', $visitor_id));
// if mapper object is hydrated copy from request body
if(!$visitor->dry()) {
$visitor->copyFrom($data);
}
$visitor->save();
}
My visitor model extends \DB\SQL\Mapper, and my $data's keys match my mapper object's properties, but $data doesn't always contain a key for every mapper object property. Does the array need to contain a key for every single mapper object property?
Anyway, the information stored in the assoc array $data does not get stored in the database.
I've also tried copyfrom() and copyFrom(). I saw both in the documentation and other examples.
Any idea what I'm doing wrong?
Does the array need to contain a key for every single mapper object property?
No.
But, array inputs are only available in F3 v3.5. If you are running v3.4 or below, you need to put your data into a hive key first.
if(!$visitor->dry()) {
$f3->set('data',$data);
$visitor->copyFrom('data');
}