xsnowy
(Zbynny)
August 8, 2016, 6:27pm
1
Hello guys,
I have yii2-user extension and also yii2-authclient installed, but when I want to register and login via Facebook, I got this message:
Integrity constraint violation – yii\db\IntegrityException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘user_id’ cannot be null
The SQL being executed was: INSERT INTO profile
(full_name
, user_id
, created_at
, updated_at
) VALUES (‘My Name’, NULL, ‘2016-08-08 18:24:08’, ‘2016-08-08 18:24:08’)
Error Info: Array
(
[0] => 23000
[1] => 1048
[2] => Column 'user_id' cannot be null
)
↵
Caused by: PDOException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘user_id’ cannot be null
in \vendor\yiisoft\yii2\db\Command.php at line 844
Where should I assign the user id? And why it’s not part of yii2-user logic?
Thanks,
xsnowy
xsnowy
(Zbynny)
August 9, 2016, 3:48pm
3
AuthController.php
protected function registerAndLoginUser($client, $userAuth)
{
/** @var \amnah\yii2\user\models\User $user */
/** @var \amnah\yii2\user\models\Profile $profile */
/** @var \amnah\yii2\user\models\Role $role */
$role = $this->module->model("Role");
// set user and profile info
$attributes = $client->getUserAttributes();
$function = "setInfo" . ucfirst($client->name); // "setInfoFacebook()"
list ($user, $profile) = $this->$function($attributes);
// calculate and double check username (in case it is already taken)
$fallbackUsername = "{$client->name}_{$userAuth->provider_id}";
$user = $this->doubleCheckUsername($user, $fallbackUsername);
// save new models
$user->setScenario("social");
$user->setRegisterAttributes($role::ROLE_USER, $user::STATUS_ACTIVE)->save();
$profile->setUser($user->id)->save();
$userAuth->setUser($user->id)->save();
// log user in
Yii::$app->user->login($user, $this->module->loginDuration);
}
here it fails ($user->id is null):
$profile->setUser($user->id)->save();
Thanks for suggestions!
amnah
(Jellysandwich5+1)
August 18, 2016, 8:10pm
4
For some reason, the user is failing to save. I imagine it’s due to some weird characters in the name or email or something. You’ll need to figure out the exact reason why using
// save new models
$user->setScenario("social");
if (!$user->setRegisterAttributes($role::ROLE_USER, $user::STATUS_ACTIVE)->save()) {
print_r($user->getErrors());
}
$profile->setUser($user->id)->save();
$userAuth->setUser($user->id)->save();
xsnowy
(Zbynny)
September 17, 2016, 3:59pm
5
amnah:
For some reason, the user is failing to save. I imagine it’s due to some weird characters in the name or email or something. You’ll need to figure out the exact reason why using
// save new models
$user->setScenario("social");
if (!$user->setRegisterAttributes($role::ROLE_USER, $user::STATUS_ACTIVE)->save()) {
print_r($user->getErrors());
}
$profile->setUser($user->id)->save();
$userAuth->setUser($user->id)->save();
Amnah, thanks for your suggestion. The error message:
Array ( [email] => Array ( [0] => Email cannot be blank. ) )
The username and e-mail from facebook is valid and also the user registered into facebook with e-mail. I tried three more users and it works for them. I also tried at http://yii2.amnahdev.com for the problematic user and the error is the same - empty e-mail.
Thanks for suggestions
Xsnowy