美文网首页
cakePHP使用笔记

cakePHP使用笔记

作者: 编程放大镜 | 来源:发表于2017-08-31 16:24 被阅读0次

    plugin

    官方资料:https://book.cakephp.org/2.0/en/plugins/how-to-install-plugins.html

    Cakephp 版本 2.4.6


    查询数据库

    find

    find(string $type = 'first', array $params = array())

    $type 可以是 'all', 'first', 'count', 'list', 'neighbors' or 'threaded',type 参数大小写敏感。

    查询所有 find('all', $params)

    $this->find('all', array('conditions' => array( ),
                    'fields'=>array('id','ctime'),
                    'order'=>'id DESC'
    ));
    
    // output
    Array(
        [0] => Array(
                [ModelName] => Array(
                        [id] => 1
                        [ctime] => value1
                    )
            )
    )
    

    conditions 例子

    'age >=' => 16,
    'age' => array(16, 17, 18),


    常用 App 静态方法

    App::build

    定义包的查找路径

    // 设置 Mysql Model 的查找路径
    App::build(array('Model'=>array(APP . 'Model' . DS . 'Mysql' . DS)));
    

    App::uses

    https://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::uses

    static App::uses(string $class, string $package)

    关于 App::uses() 这个静态方法,官方文档是这么说的:

    Classes are lazily loaded in CakePHP, however before the autoloader can find your classes you need to tell App, where it can find the files.
    By telling App which package a class can be found in, it can properly locate the file and load it the first time a class is used.

    也就是说 App::uses() 只是定义了要加载的类和类文件的路径,并没有将类文件加载进来。只有在第一次使用这个类时才会去加载。

    Components

    Components are packages of logic that are shared between controllers.

    • 想在 Component 中使用 set 定义 views中的变量。
      google之后参考原生的PaginatorComponent,添加一下代码实现。
        public function __construct(ComponentCollection $collection, $settings = array()) {
            $settings = array_merge($this->settings, (array)$settings);
            $this->Controller = $collection->getController();
            parent::__construct($collection, $settings);
        }
    
    • 如何在 controller 中手动加载 component
    $cptName = $this->Components->load('componentName');
    

    相关文章

      网友评论

          本文标题:cakePHP使用笔记

          本文链接:https://www.haomeiwen.com/subject/wkmqjxtx.html