CakePHP3のfind結果はdebug関数で見ると良い
CakePHP3を触り始めています。Cake3からfind()の結果がオブジェクトになりました。
Cake2までは配列だったのでpr関数で見ても問題なかったのですが、オブジェクトになるとprは辛くなります。
CakePHP3からはdebug関数を使うと下記のようにリレーション先のデータも確認できるようになります。debug関数はcakephpが標準で用意しているグローバル関数です。CakePHP2にもあります。
debug( $this->Users->find()->contain(['Bookmarks'])->all() );
Users hasMany Bookmarksの状態でfindすると、下記のようなデータがdebug()を通して確認できます。
Usersオブジェクトの中にitemsフィールドがあり、その中身が下記になります。
リレーション先のエンティティオブジェクトがbookmarksフィールドに入っていて、その値も確認できます。
(int) 0 => object(App\Model\Entity\User) { 'properties' => [ 'id' => (int) 1, 'email' => 'xxxxxxx@gmail.com', 'password' => 'xxxx', 'created' => object(Cake\I18n\Time) { 'time' => '2014-12-23T06:48:20+0000', 'timezone' => 'UTC', 'fixedNowTime' => false }, 'updated' => null, 'bookmarks' => [ (int) 0 => object(App\Model\Entity\Bookmark) { 'properties' => [ 'id' => (int) 1, 'user_id' => (int) 1, 'title' => 'aaa', 'description' => 'aaa', 'url' => '', 'created' => object(Cake\I18n\Time) { 'time' => '2014-12-23T07:11:21+0000', 'timezone' => 'UTC', 'fixedNowTime' => false }, 'updated' => null ], 'dirty' => [], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Bookmarks' } ] ],
Debug用のクラス(Cake\Error\Debugger)など他にも用意されていて、ドキュメントも下記にあります。
http://book.cakephp.org/3.0/en/development/debugging.html