Php 无法使用用户id以编程方式登录
我的用户模型中有hybridRegister回调。一旦用户使用socal登录(例如facebook)注册,我就会尝试查看他的电子邮件。若数据库中存在电子邮件,我将停止注册并使用与此电子邮件相关的用户数据进行登录Php 无法使用用户id以编程方式登录,php,cakephp,cakephp-2.3,Php,Cakephp,Cakephp 2.3,我的用户模型中有hybridRegister回调。一旦用户使用socal登录(例如facebook)注册,我就会尝试查看他的电子邮件。若数据库中存在电子邮件,我将停止注册并使用与此电子邮件相关的用户数据进行登录 public function hybridRegister($provider, Hybrid_User_Profile $profile) { $profile = (array)$profile; $data = array( 'provide
public function hybridRegister($provider, Hybrid_User_Profile $profile) {
$profile = (array)$profile;
$data = array(
'provider' => $provider,
'provider_uid' => $profile['identifier'],
//Extra fields here as needed like group_id, status etc.
);
if (strtolower($provider) === "vkontakte"){
$data['firstName'] = $profile['firstName'];
$data['lastName'] = $profile['lastName'];
//TODO make fetching picture to our server
$data['photo'] = $profile['photoURL'];
$data['email'] = $profile['emailVerified'];
}
if (strtolower($provider) === "facebook"){
$data['firstName'] = $profile['firstName'];
$data['lastName'] = $profile['lastName'];
//TODO выкачивать картинку на сервер
$data['photo'] = $profile['photoURL'];
$data['email'] = $profile['emailVerified'];
}
//Check if an email exists in our db. if yes, then login user else
// continue register...
$user = $this->existEmail($data);
if ($user){
**//AuthComponent::login throw error somewhere inside it**
AuthComponent::login($user);
return true;
}
else {
// если нет, то продолжаем регистрацию
$data['password'] = $this->generatePassword();
$data['username'] = $data['provider'] . $profile['identifier'];
$this->sendRegistrationEmail($data['email'], $data['username'], $data['password']);
$this->create();
return $this->save($data, false);
}
}
下面是错误:
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_setDefaults' at line 1
SQL Query: _setDefaults
Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp
Stack Trace
CORE/Cake/Model/Datasource/DboSource.php line 460 → PDOStatement->execute(array)
CORE/Cake/Model/Datasource/DboSource.php line 426 → DboSource->_execute(string, array)
CORE/Cake/Model/Datasource/DboSource.php line 668 → DboSource->execute(string, array, array)
CORE/Cake/Model/Datasource/DboSource.php line 611 → DboSource->fetchAll(string, array, array)
CORE/Cake/Model/Model.php line 827 → DboSource->query(string, array, User)
CORE/Cake/Controller/Component/AuthComponent.php line 604 → Model->__call(string, array)
CORE/Cake/Controller/Component/AuthComponent.php line 604 → User->_setDefaults()
APP/Model/User.php line 250 → AuthComponent->login(array)
[internal function] → User->hybridRegister(string, Hybrid_User_Profile)
我已经研究了authComponent代码(一个登录函数)。有一个描述
/**
* Log a user in.
*
* **If a $user is provided that data will be stored as the logged in user.** If `$user` is empty or not
* specified, the request will be used to identify a user.
我不明白为什么我的代码不起作用。请告诉我
回答
hybridRegister函数的代码应该更改一点。一旦数据库中的现有用户电子邮件获得批准,函数应该只返回用户。HybridAuth使auth本身成为auth。代码如下:
//Check if an email exists in our db. if yes, then login user else
// continue register...
$user = $this->existEmail($data);
if ($user){
return $user;
}
我的错误是认为hybridRegister必须保存用户或返回false。但它可以从数据库中获取任何现有的用户记录并返回它
感谢@AD7six和@burzum的想法。而@AD7six的回答对我来说是最有帮助的 检查SQL错误和堆栈跟踪,如图所示
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_setDefaults' at line 1
SQL Query: _setDefaults
CORE/Cake/Controller/Component/AuthComponent.php line 604 → User->_setDefaults()
很明显,您正在调用一个在该模型对象中不存在的方法_setDefaults()——猜测它是一个模型。在2.x中,当它是一个模型对象时,不存在的方法被转换成SQL查询
然而。我猜它是试图在模型上调用该方法,而不是组件或类似的东西。调试愉快
我的用户模型中有hybridRegister回调
那是行不通的
登录函数不是静态的
不起作用的原因是,它调用了其他实例方法:
public function login($user = null) {
$this->_setDefaults(); # <-
其中,
hybridRegiser
被修改为仅负责创建用户记录(或不创建用户记录),如果需要,发送注册电子邮件,并根据需要返回true/false。谢谢。你说的“它不是静态的”是对的。但是,如果数据库中已经存在电子邮件,我需要中断默认的hybridRegister函数,然后使用CakePhp提供的简单登录/通过身份验证机制登录用户。我修改了我的问题,以向您展示我在我的应用程序中使用两种身份验证方法。请阅读-您的身份验证对象本身并不调用AuthComponent::anything-如果身份验证对象无法识别用户,则应返回false。以及一系列用户信息(如果可以)。
。除此之外,HybridAuth
的代码不在问题中。谢谢。我不知道“将不存在的2.x方法转换为SQL查询”。我在问题中添加了其他信息。如果您有时间,请查看更新部分。谢谢。
public function login($user = null) {
$this->_setDefaults(); # <-
public function register() {
if ($this->User->hybridRegister($this->request->data)) {
$id = $this->User->id;
$this->request->data['User'] = array_merge(
$this->request->data['User'],
array('id' => $id)
);
unset($this->request->data['User']['password']);
$this->Auth->login($this->request->data['User']);
return $this->redirect('/users/home');
}
}