概要
YiiのデベロッパーツールのログでActiveRecordの中身を出力した時、見にくかったので変更してみました。
環境
Version | |
---|---|
PHP | 7.3 |
Yii | 2 |
変更箇所
vendor/yiisoft/yii2/helpers/BaseVarDumper.php
line.197
// 変更前のコード
$output = 'unserialize(' . var_export(serialize($var), true) . ')';
// 変更後のコード
$originObj = clone $var;
$output = print_r((array) $originObj, true);
出力サンプル
+------+-----------+------------+------------------------+
| id | last_name | first_name | email |
+------+-----------+------------+------------------------+
| 1 | 苗字 | 名前 | xxxxxxxxxxxxxx@xxx.com |
+------+-----------+------------+------------------------+
例として、上記のデータを取得して出力を行ってみます。
$loginUser = LoginUser::find(['id' => 1])->all();
Yii::info($loginUser);
変更前
変更前は ActiveRecordの内容が1行で表示され、わかりにくいです。
unserialize('O:23:"common\\models\\LoginUser":12:{s:9:"logBuffer";s:0:"";s:11:"' . "\0" . '*' . "\0" . 'appTypes";a:2:{i:0;s:11:"app-backend";i:1;s:12:"app-frontend";}s:36:"' . "\0" . 'yii\\db\\BaseActiveRecord' . "\0" . '_attributes";a:4:{s:2:"id";i:1;s:9:"last_name";s:6:"苗字";s:10:"first_name";s:6:"名前";s:5:"email";s:22:"xxxxxxxxxxxxxx@xxx.com";}s:39:"' . "\0" . 'yii\\db\\BaseActiveRecord' . "\0" . '_oldAttributes";a:4:{s:2:"id";i:1;s:9:"last_name";s:6:"苗字";s:10:"first_name";s:6:"名前";s:5:"email";s:22:"xxxxxxxxxxxxxx@xxx.com";}s:33:"' . "\0" . 'yii\\db\\BaseActiveRecord' . "\0" . '_related";a:0:{}s:47:"' . "\0" . 'yii\\db\\BaseActiveRecord' . "\0" . '_relationsDependencies";a:0:{}s:23:"' . "\0" . 'yii\\base\\Model' . "\0" . '_errors";N;s:27:"' . "\0" . 'yii\\base\\Model' . "\0" . '_validators";N;s:25:"' . "\0" . 'yii\\base\\Model' . "\0" . '_scenario";s:7:"default";s:27:"' . "\0" . 'yii\\base\\Component' . "\0" . '_events";a:2:{s:12:"beforeInsert";a:1:{i:0;a:2:{i:0;a:2:{i:0;O:34:"common\\behaviors\\TimestampBehavior":7:{s:18:"createdAtAttribute";s:7:"created";s:18:"updatedAtAttribute";s:7:"updated";s:5:"value";N;s:10:"attributes";a:2:{s:12:"beforeInsert";a:2:{i:0;s:7:"created";i:1;s:7:"updated";}s:12:"beforeUpdate";s:7:"updated";}s:17:"skipUpdateOnClean";b:1;s:22:"preserveNonEmptyValues";b:0;s:5:"owner";r:1;}i:1;s:18:"evaluateAttributes";}i:1;N;}}s:12:"beforeUpdate";a:1:{i:0;a:2:{i:0;a:2:{i:0;r:25;i:1;s:18:"evaluateAttributes";}i:1;N;}}}s:35:"' . "\0" . 'yii\\base\\Component' . "\0" . '_eventWildcards";a:0:{}s:30:"' . "\0" . 'yii\\base\\Component' . "\0" . '_behaviors";a:1:{i:0;r:25;}}')
変更後
変更後は、配列として表示されるので大変見やすくなりました。
[
Array
(
[logBuffer] =>
[*appTypes] => Array
(
[0] => app-backend
[1] => app-frontend
)
[yii\db\BaseActiveRecord_attributes] => Array
(
[id] => 1
[last_name] => 苗字
[first_name] => 名前
[email] => xxxxxxxxxxxxxx@xxx.com
)
[yii\db\BaseActiveRecord_oldAttributes] => Array
(
[id] => 1
[last_name] => 苗字
[first_name] => 名前
[email] => xxxxxxxxxxxxxx@xxx.com
)
[yii\db\BaseActiveRecord_related] => Array
(
)
[yii\db\BaseActiveRecord_relationsDependencies] => Array
(
)
[yii\base\Model_errors] =>
[yii\base\Model_validators] =>
[yii\base\Model_scenario] => default
[yii\base\Component_events] => Array
(
)
[yii\base\Component_eventWildcards] => Array
(
)
[yii\base\Component_behaviors] =>
)
]