要对用户进行身份验证,您应该使用 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