LoginSignup
4
4

More than 3 years have passed since last update.

cakephp3 チートシート

Last updated at Posted at 2020-09-02

自分用です。
忘れてぐぐりがちなこと覚え書き
phpの関数とかもあるかもしれない
随時更新予定

クックブック
https://book.cakephp.org/3/ja/index.html

クエリビルダー

https://book.cakephp.org/3/ja/orm/query-builder.html
https://www.ritolab.com/entry/67
https://qiita.com/morisuke/items/e466d2ab360ab5646e9a

クエリ


$query = $this->HogeModel->find()     //ここまでテンプレ
            ->select(['id', 'title']) //取得するフィールド名
            ->where($conditions)      //取得条件
            ->order($order)           //order
            ->limit($limit);          //取得件数

取得するデータ形式指定


$result1 = $query->all();     //全件取得(返り値はObject型)
$result2 = $query->toList();  //全件取得(返り値はList型)
$result3 = $query->toArray(); //全件取得(返り値はArray型)
$result4 = $query->first();   //先頭の一件のみ取得

conditionの書式

AND


//where id = 0 and title = 'aaa'
$conditions = [
    'Hoge.id'    => 0,
    'Hoge.title' => 'aaa'
];

OR


//where id = 0 or title = 'aaa'
$conditions = [
    'OR' => [
        'Hoge.id'    => 0,
        'Hoge.title' => 'aaa'
     ]
];

NOT


//下記両方とも結果は同じ
//where id is not 0
$conditions = [
    'Hoge.id IS NOT' => 0
];
//where id != 0
$conditions = [
    'Hoge.id !=' => 0
];

IN


//where id in (0, 1, 2)
$conditions = [
    'Hoge.id IN' => [0, 1, 2]
];

joinの書式


$join = [
    'table'      => 'fuga',                 //joinするテーブル名(Model名ではないので注意)
    'alias'      => 'f',                    //エイリアス
    'type'       => 'inner',                //タイプ(inner || left || right)
    'conditions' => ['f.id = Hoge.fuga_id'] //一致条件
];

//fieldを指定しないとjoinしたテーブル(ここではfuga)のデータが取れなかった…仕様?
$fields = [
    'hoge_id'    => 'Hoge.id',
    'hoge_title' => 'Hoge.title',
    'fuga_id'    => 'f.id',
];
$result = $this->find()->join($join)->select($fields)->all();

ログ

$this->log()はControllerでのみ使える

Controllerでの使い方


$this->log("message", LOG_DEBUG); //デバッグログ
$this->log("message", LOG_ERROR); //エラーログ

それ以外での使い方


use Cake\Log\Log;
Log::debug("message"); //デバッグログ
Log::error("message"); //エラーログ

パスの設定

デフォルトパスは{PJルート}/logs/以下で、app.phpの下記設定を変更すればパスを変更できる
下記の「path」の値を変えればOK
LOGSは定数で、上記デフォルトパスを示す

app.php
 /**
  * Configures logging options
  */
'Log' => [
    'debug' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'debug',
        'url' => env('LOG_DEBUG_URL', null),
        'scopes' => false,
        'levels' => ['notice', 'info', 'debug'],
    ],
    'error' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'error',
        'url' => env('LOG_ERROR_URL', null),
        'scopes' => false,
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
    ],
]

ログが出てない!

出力パスがデフォルトなのにログが出力されない場合、権限が原因の可能性が高い。

sudo chmod -R 777 logs

とりあえずこれ叩いてログが出るか確認、出てたら適切な権限にちゃんと修正

キャッシュ削除

コマンドから削除

$ cd {PJルート}
$ bin/cake cache clear default //特定のprefixだけ削除
$ bin/cake cache clear_all //全部削除

メソッドで削除

use Cake\Cache\Cache;
Cache::delete(string $key , string $config = 'default');

clearだと「特定configのキャッシュを全て削除」になるっぽい

php.iniの場所

$ php --ini
or
$ php -i | grep php.ini
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4