findメソッド(CakePHP)
CakePHPのデータ取得、findメソッドについて。
$posts = $this->Posts->find('all');
全件のpostsテーブルからのデータ取得
$posts = $this->Posts->find('all'); $this->set(compact('posts'));
で全件をビュー側に渡してやる。
$posts = $this->Posts->find('all'); $this->set('post', $query->first());
一つ目の結果をビューに渡す。
$posts = $this->Posts->find('all', ['conditions' => ['body like' => '%おはよう%']]);
オプションの設定。 おはようという文字を含むデータをbodyカラムから取得。
他にも配列で表示できるarrayと組み合わせたりとか、 色々できるようです。
複雑なデータ取得もできるようになりたいです。