FuelPHPのデータベース関係をまとめてみた(SELECT編)
公開日:
:
FuelPHP

FuelPHPのデータベースへの接続とビューへの受け渡しについて、まとまった記載がみつからないので自分でまとめてみました。今回はSELECT編です。UPDATEとかは次回です。
FuelPHPでデータベースからSELECTしてVIEWにデータを渡す。よくある日常風景です。FuelPHPにはDBからSELECTする幾つかの柔軟?な方法が用意されています。便利な半面、VIEWに渡す・VIEWで読み出す段階で混乱して間違えてしまうことがあります。私にはよくあります。そこで、今回はそのあたりをまとめてメモしていきたいと思います。SELECT関係でも難しそうな部分はスルーしてますのでご容赦くださいね。
間違っているところがあればコメントで教えてください。お願いします。そして順次追記していくことになりますのでご了承ください。
公式サイトの該当するページヘリンクを貼ってあります。英文ですが、発行されるSQL文が書いてあるのでSQLとPHPの知識で理解できると思います。(経験談)
DBクラスを使ってSQLをそのまま書く方法
$query = DB::query(‘SELECT * FROM users WHERE id = 5’);
$query->execute();
$result = DB::query(‘SELECT * FROM `users`’)->execute();
クエリビルダを使う方法
参考ページ
http://fuelphp.com/docs/classes/database/qb.html
$result = DB::select(‘id’,’name’)->from(‘users’)->execute();
$colums = array(‘id’, ‘name’);
$result = DB::select_array($columns)->from(‘users’)->execute();
$result = DB::select(array(‘name’,’the_name’))->from(‘users’)->execute();
$users = DB::select(‘name’)->from(‘users’)->distinct(true)->execute();
実験してみると ->execute()->as_array(); を付けないと「Fuel\Core\FuelException [ Error ]: Database results are read-only」と怒られて止まります。なんだろ?
$result = DB::select()->from(‘users’)->where(‘id’, 1)->execute();
$result = DB::select()->from(‘users’)->where(‘id’, ‘=’, 1)->execute();
$result = DB::select()->from(‘users’)->where(‘id’, ‘!=’, 1)->execute();
$id_array = array(1,2,3);
$result = DB::select()->from(‘users’)->where(‘id’, ‘in’, $id_array)->execute();
$result = DB::select()->from(‘users’)->where(‘id’, ‘between’, array(1, 2))->execute();
$who = “john%”;
$result = DB::select()->from(‘users’)->where(‘id’, ‘like’, $who)->execute();
DB::select()->from(‘users’)->limit(1);
DB::select()->from(‘users’)->limit(10)->offset(5);
DB::select(‘users’)->order_by(‘id’,’asc’)->limit(10);
DB::select()->from(‘users’)->order_by(‘name’);
DB::select()->from(‘users’)->order_by(‘name’,’asc’);
DB::select()->from(‘users’)->order_by(‘name’,’asc’)->order_by(‘surname’, ‘desc’);
$result = DB::select(‘id’,’name’)->from(‘users’)->as_assoc()->execute();
$result = DB::select(‘id’,’name’)->from(‘users’)->as_object()->execute();
$result = DB::select()->from(‘users’)->as_object(‘Model_Users’)->execute();
$result = DB::select()->from(‘users’)->as_object(‘\\Users\\Model_Users’)->execute();
$result = DB::select()->from(‘users’)->where_open()
->where(‘name’, ‘John’)
->and_where(‘email’, ‘john@example.com’)
->where_close()
->or_where_open()
->where(‘name’, ‘mike’)
->or_where(‘name’, ‘dirk’)
->or_where_close()->execute();
DB::select(‘title’,’content’)->from(‘articles’)->execute()->get(‘title’);
$result = DB::select(‘*’)->from(‘users’)->execute();
$num_rows = count($result);
ビュー(VIEW)で表示する方法
$result = DB::select()->from(‘users’)->execute()->as_array();
VIEW–
<?php foreach($result as $item): ?>
<?php echo $item[‘name’]; ?>
<?php endforeach; ?>
$result = DB::select()->from(‘users’)->execute()->as_array();
foreach($result as $item)
$result_array = $result->as_array();
foreach($result_array as $item)
$result = DB::select()->from(‘users’)->execute();
$on_key = $result->as_array(‘id’);
foreach($on_key as $id => $item)
$key_value = $result->as_array(‘id’, ‘email’);
foreach($key_value as $id => $email)
Model_Crudを使う方法
参考ページ
http://fuelphp.com/docs/classes/model_crud/methods.html
$entry = Model_Article::find_all();
$data['entry'] = Model_Article::find(array(
'where' => array('category_id', 1),
'order_by' => array('date' => 'desc'),
'limit' => 10
));
-- VIEW --
<?php foreach ($query as $row): ?>
<?php echo $row->title; ?>
$data['rows'] = Model_Post::find_all(); -- VIEW -- <?php foreach ($rows as $row): ?> <?php echo $row['title']; ?>
Ormパッケージを使う方法
参考ページ
http://fuelphp.com/docs/packages/orm/crud.html
$entry = Model_Article::find('all');
$entry = Model_Article::find('all', array(
'where' => array('category_id', 1),
'order_by' => array('date', 'desc')
));
$data[‘article’] = Model_Article::find()
->order_by(‘id’, ‘desc’)
->get();
— VIEW —
<php echo $article->title; ?>
<php echo $article->user->name; ?>
Sponsored Link
関連記事
-
-
ぶっちぎりだぜ!Panasonicと気になるニュースのメモ
ブラシレスジンバルのモニターをスマートフォンでプレビューしようとGH3のWiFi接続に挑戦しています
-
-
【雑談】3Dプリンターでカメラリグを作りたい
まったくの妄想記事なので、お暇な方だけ読んで下さい。特別な情報は書かれていませんので予めご了承くださ
-
-
NOKTON来たからテスト撮影したらスライダーが欲しい
やけに寒いと思って起きたら外は真っ白^^; 4月なのに雪です。あと数日後なら綺麗な桜に雪景色だったの
-
-
エーディテクノCL5585H一眼レフカメラ用液晶モニター5型を開封の儀
急遽、外部モニターが無いと不便なので購入しました。とりあえず安いの買って、これから出てくるPanas
-
-
到着したのは Godox 500 LED プロフェッショナルビデオライトだが…
あまり国内では見かけない Godox 500 LED Professionl Video Light





ご質問は掲示板へお願いいたします。