Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

on3nx's avatar
Level 1

Data disappear when i do model->save([...])

i did below in my model class:

 private function createToken($user){
        $token = $this->getToken();
        $this->save([
            'user_id' => $user->id,
            'token' => $token
        ]);
        return $token;
}

it's obvious that i put user id and token there to be inserted to my DB.

however, once i run the program, it throw me an error:

Field 'user_id' doesn't have a default value

and i found out that my user_id and token disappear from the query, as you can see in the error stack below:

in Connection.php line 761
at Connection->runQueryCallback('insert into `user_tokens` (`updated_at`, `created_at`) values (?, ?)', array('2016-09-27 15:22:20', '2016-09-27 15:22:20'), object(Closure)) in Connection.php line 717
at Connection->run('insert into `user_tokens` (`updated_at`, `created_at`) values (?, ?)', array('2016-09-27 15:22:20', '2016-09-27 15:22:20'), object(Closure)) in Connection.php line 481
at Connection->statement('insert into `user_tokens` (`updated_at`, `created_at`) values (?, ?)', array('2016-09-27 15:22:20', '2016-09-27 15:22:20')) in Connection.php line 435
at Connection->insert('insert into `user_tokens` (`updated_at`, `created_at`) values (?, ?)', array('2016-09-27 15:22:20', '2016-09-27 15:22:20')) in Processor.php line 32
at Processor->processInsertGetId(object(Builder), 'insert into `user_tokens` (`updated_at`, `created_at`) values (?, ?)', array('2016-09-27 15:22:20', '2016-09-27 15:22:20'), 'id') in Builder.php line 2142
at Builder->insertGetId(array('2016-09-27 15:22:20', '2016-09-27 15:22:20'), 'id')
at call_user_func_array(array(object(Builder), 'insertGetId'), array(array('updated_at' => '2016-09-27 15:22:20', 'created_at' => '2016-09-27 15:22:20'), 'id')) in Builder.php line 1433
at Builder->__call('insertGetId', array(array('updated_at' => '2016-09-27 15:22:20', 'created_at' => '2016-09-27 15:22:20'), 'id')) in Model.php line 1607
at Model->insertAndSetId(object(Builder), array('updated_at' => '2016-09-27 15:22:20', 'created_at' => '2016-09-27 15:22:20')) in Model.php line 1576
at Model->performInsert(object(Builder)) in Model.php line 1472
at Model->save(array('user_id' => '2', 'token' => '219bcefd2c8d745f8f010216b18cd96d59d509b8a8af5ec07cecd1cd220e95b1')) in Activation.php line 83
at Activation->createToken(object(User)) in Activation.php line 59

Am I missed anything here?

Thank you

0 likes
5 replies
InaniELHoussain's avatar
Level 32

what about this

private function createToken($user){
    $token = $this->getToken();
$this->user_id = $user->id
    $this->save([
        'token' => $token
    ]);
    return $token;
}
1 like
on3nx's avatar
Level 1

@InaniELHoussain Thanks for reply,

I just tried that, it's not work, obviously User_ID won't be sent to database.

at Model->save(array(/*no user id here*/'token' => '525323523')) in Activation.php line 83

thanks

Jaytee's avatar

$this->user_id = $id

// same thing for token here

$this->save();

1 like
on3nx's avatar
Level 1

Hi @InaniELHoussain & @Jaytee ,

Thanks! it works now!

you guys are awesome!

:)

full answer:

private function createToken($user){
    $token = $this->getToken();
    $this->user_id = $user->id;
    $this->token = $token;
    $this->email = $user->email;
    $this->save();
    return $token;
}
1 like

Please or to participate in this conversation.