我正在尝试在cakephp 3中进行登录系统。
以下是查询:
$user_details = $this->User->find('first', ['conditions'=>['email_id'=$email, 'password'=>$password]]);
if(!empty($user_details)){
$this->request->session()->write('user_email'=>$user_details['email_id']);
$this->request->session->write('user_id'=>$user_details['id');
}
你能说出cakephp 2和cakephp 3在编写查询方面的差异吗?
答案 0 :(得分:0)
尝试此查询,它的工作正常...
$user_details = $this->User->find('first', ['conditions'=>['User.email_id'=$email, 'User.password'=>$password]]);
否则你会从http://book.cakephp.org/3.0/en/controllers/components/authentication.html
获得帮助答案 1 :(得分:0)
要对用户进行身份验证,您应该使用身份验证组件。这是CakePHP中的最佳实现,因为它会自动将所有数据绑定到服务器并请求。
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'plugin' => 'Users'
],
'authError' => 'Did you really think you are allowed to see that?',
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email']
]
],
'storage' => 'Session'
]);
}
请参阅文档:http://book.cakephp.org/3.0/en/controllers/components/authentication.html
回答原始问题
Cake 2使用带有数组结构的自动化函数来构建查询,这非常狡猾。查询将在调用时直接执行。
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'recursive' => 1, //int
//array of field names
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
//string or array defining order
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'), //fields to GROUP BY
'limit' => n, //int
'page' => n, //int
'offset' => n, //int
'callbacks' => true //other possible values are false, 'before', 'after'
)
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
在Cake 3中,使用查询构建器对象构建查询。这会在每次后续调用时修改SQL。它只在您调用它后执行。
$query = $articles->find('all')
->where(['Articles.created >' => new DateTime('-10 days')])
->contain(['Comments', 'Authors'])
->limit(10);
此处,每次调用时都会使用引用的SQL操作对象(where
,contain
,limit
)。
在您应用execute()
,first()
,toArray()
等后执行查询,其中toArray()
将数据集作为数组返回,其他人作为对象返回。
进一步阅读:http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html