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
関連記事
-
-
GMMAは複合型移動平均線で群馬じゃないから
テクニカル分析ってやつ。MetaTraderで使うインジケータってやつが優秀。これでトレンドを判断す
-
-
録音(撮影)ミニスタジオの制作DIY
そんな訳で吸音スポンジを壁に取り付けてみた続きです。 調子に乗って汚い作業部屋を半分にして、収
-
-
寒いから TARION® Vマウントバッテリー用 電源プレートを確認
凄い晴天。早速レンズアダプターを付けて撮影してみようかどうかと作業場へ向かうと...凄い風と低い気温
-
-
情報をいくら仕入れても使いこなすのは難しいと気がついた結果
プロカメラマンが教える! 知っておきたい写真のこと100という記事を見ました。題名の通り100個もの
-
-
ネイティブライブラリの拡張(継承)
ネットで検索しまくってやっとみつけた「ネイティブライブラリの拡張(継承)」。 CodeIgniter
ご質問は掲示板へお願いいたします。