I have a Samples Table in my database. Here is my Association in SamplesTable.php
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
In my SamplesControler.php add method i'm getting current User data like this:
$users = $this->Samples->Users->get($this->Auth->user('id'), [
'keyField' => 'id',
'valueField' => 'name'
]);
$this->set(compact('sample', 'users'));
In my add.ctp in Template/Samples/ i'm using this variable to show the user name like this:
echo $this->Form->input('user_id', [
'label' => 'Client',
'type' => 'text',
'disabled' => true,
'value' => $users->name,
'required' => true,
]);
It shows the name correctly, but when i hit submit it doesn't returns the id of the user back to the controller to save it in database. Since i'm very new to this framework i can't figure out if there is anything i'm doing wrong.
I've tried debug response data in my controller add method and there is no user_id in response.
if i change
$users->name to $users
in my template file, it shows all the data about that user in json format in the textfield like this:
{ "id": 4, "name": "firstname lastname", "title": "Engineer", "street": "122", "city": "Lahore", "state": "Punjab", "email_primary": "[email protected]", "created": "2016-03-04T07:45:42+0000", "modified": "2016-03-04T09:56:17+0000"}
insert) a record into the database, I usually don't have itsiduntil after the database save/transaction has happened. It almost sounds like you are doing some sort ofupdate, instead. - summea