I am not even sure how to explain this properly so i will just give you the code differences.
Bascially I am making AJAX call to a controller, and in the controller, I am saving a new item to database then returing that new object.
Here is the controller.
class NotesController extends ControllerBase
{
public function addNoteAction()
{
if($this->request->isPost()){
$data = $this->request->getPost();
if ($data['note'] != ''){
$noteUser = $this->auth->getIdentity();
$note = new Notes();
$note->assign(array(
'recruitId' => $data['id'],
'userId' => $noteUser['id'],
'note' => $data['note']
));
if(!$note->save()){
echo json_encode(false);
} else {
$note->name = $note->user->name;
echo json_encode($note);
}
}
}
}
}
Note Model (the important bits)
public function initialize()
{
$this->belongsTo('recruitId', 'Join\Models\Recruits', 'id', array(
'alias' => 'recruit'
));
$this->belongsTo('userId', 'Join\Models\Users', 'id', array(
'alias' => 'user'
));
}
public function beforeValidationOnCreate(){
$this->noteDate = date("Y-m-d H:i:s");
}
The response to this as is results in the following JSON returning to the browser:
{
"id": "225",
"userId": "2",
"recruitId": "2",
"note": "asdfasdfadsf",
"noteDate": "2015-09-17 13:19:31",
"user": {
"id": "2",
"name": "Chrsitian allred",
"email": "xxxxxxxxxxxx",
"phone": "xxxxxxxxxx",
"password": "xxxxxxx",
"active": "1",
"banned": "0"
},
"name": "Chrsitian allred"
}
Obviously i am not crazy about having my encrypted password sent back to the brwoser.
What is really odd, is when i remove the php $note->name = $note->user->name
the result is this:
{
"id": "225",
"userId": "2",
"recruitId": "2",
"note": "asdfasdfadsf",
"noteDate": "2015-09-17 13:19:31",
}
Why is the user Ojbect getting attached to the Note object only after I attach a custom variable?