2016-02-04 44 views
0

我试图做的登录系统CakePHP中3怎么写的MySQL查询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到3的CakePHP有关分歧写查询?

+0

**“不工作”不是正确的问题陈述**。请提供比此更好的故障排除信息。告诉我们你做了什么,并发布所有涉及的代码,包括模型,控制器和视图。请阅读[我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask) –

回答

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


要回答你原来的问题

蛋糕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

在蛋糕3的查询使用查询生成器对象建立。这会在每次后续调用中修改SQL。它只在你调用之后执行。

$query = $articles->find('all') ->where(['Articles.created >' => new DateTime('-10 days')]) ->contain(['Comments', 'Authors']) ->limit(10);

在这里,一个对象与在每次调用(wherecontainlimit)引用的SQL操纵。在应用​​,first()toArray()等,其中toArray()返回数据设置为阵列和其他人作为对象之后执行

查询。

延伸阅读:http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html