0

我正在尝试在 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 关于编写查询的区别吗?

4

2 回答 2

0

要对用户进行身份验证,您应该使用 Authentication 组件。这是 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);

这里的对象在每次调用 ( where, contain, limit) 时都使用引用的 SQL 进行操作。

在您应用execute(),等后执行查询first()toArray()其中toArray()将数据集作为数组返回,其他作为对象返回。

进一步阅读:http ://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

于 2016-02-05T09:32:33.550 回答
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获得帮助

于 2016-02-04T12:55:19.920 回答