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
関連記事
-
-
5.5万円のアイスクリーム型ブラシレスジンバル Beholder登場
GWもそろそろ終わり。やっと道路が普通に戻ってくれます。この時期、県外からや色々と、運転が危ない人が
-
-
SimpleBGC Mobile について調べてみた
ペヤングが食べられないのは寂しい気もするけど、こうなると、売っていても購入することも無いでしょう。歳
-
-
「ソーシャルメディアで伝わる文章術」って日本語おかしくないのかな?
本の題名はともかく、内容はヒントに溢れる良書でした。文章の書きかたから、はては人生の教訓まで含んだ内
-
-
Amazonで売っている激安DSLR用LCDディスプレモニター購入時の注意
Amazonで激安中華製品がかなり目立つようになってきました。特にカメラ用のHDMI端子が付いたLC
-
-
はじめての Xcode
習うより慣れろ! ということでWEBを巡回しても???な頭じゃ、先が思いやられるといきなりXcode
ご質問は掲示板へお願いいたします。