cakephp总结

参考资料

CakePHP 2.x 菜谱:访问速度比cakePHP官网快很多

立刻渲染view

$this->set('title_for_layout','提示');
$this->set('error', $errMsg);
$this->render('/Elements/ajax');
$this->response->send();
$this->_stop();

调试

在 controller 中设置debug级别为2

public function beforeFilter(){
		parent::beforeFilter();
		Configure::write('debug', 2);
	}

打印sql语句

debug($this->ModelName->getDataSource()->getLog(false, false)); exit;
#或者
ConnectionManager::getDataSource('default')->getLog(false, false);
#直接输出sql
ConnectionManager::getDataSource('default')->showLog();
#在model里可以这样写
$this->getDataSource()->showLog();

全局常量和方法

pr()
<pre>标签的 print_r。 Convenience wrapper for print_r(), with the addition of wrapping <pre> tags around the output.

常用cakephp自带函数

Hash提取二维数组

$users = Array(
     Array(
        'id' => 123,
        'name'=> 'fred',
        'surname' => 'bloggs'
     ),
     Array(
        'id' => 245,
        'name' => 'fred',
        'surname' => 'smith'
     ),
     Array(
        'id' => 356,
        'name' => 'joe',
        'surname' => 'smith'
      )
   );
$ids = Hash::extract($users, '{n}[name=fred].id');
// $ids will be array (123, 245)

注意:{n}后面没有.

原文地址:https://www.cnblogs.com/aworkstory/p/12485123.html