Assigning new variable to object, causes different json encodings

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?

edited Sep '15

Same things happens when i add it to afterFetch instead.

public function afterFetch(){
    $this->name = $this->user->name;
}


10.2k

Not sure why the entire user object is getting appended. That's weird.

To hide the password from the json response, make it protected in the user model.

edited Sep '15

Yes i did that zach, but i am more or less posting this as a potential bug. Not sure why the entire object is all teh sudden getting appended because i did some after the fact object edits.