I want to save the users user_id
along the data he submits via a form. This is in my controller:
The controller:
$request = $this->getRequest();
if ($request->isPost()) {
$testing = new Testing();
$form->setInputFilter($testing->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->zfcUserAuthentication()->hasIdentity(); // user logged in?
$user = $this->zfcUserAuthentication()->getIdentity(); //get identity
$user_id = $user->getID(); //gets the user-id, output: number, eg. 18
$testing->exchangeArray($form->getData());
$this->getTestingTable()->saveTesting($testing);
// Redirect to iaps test
return $this->redirect()->toUrl('/testing/iaps4');
}
}
return array('form' => $form);
}
If I print_r($user_id)
the output will be plain and simple 18
.
I tried several things that did not work, here the most promising ones:
exchangeArray
for the user_id only. Unfortunately, it
only generates an empty entry in my database along the data form.$testing->exchangeArray($form->getData());
with the
user_id data, obviously no success here either.The thing is, I kinda know what I need: the plain output 18
from the $user->getID();
won't help me, because I need to asign this number to the user_id
column in the database along the data submitted by the user with the id user_id
.
I'm using Zend Framework 2.3.3, ZF-Commons/ZfcBase 0.0.1 and ZF-Commons/ZfcUser 1.2.2
You can merge user_id with the data from your form in a single array with array_merge :
$testing->exchangeArray(array_merge(
$form->getData(),
['user_id' => $user->getID()]
));