*

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

公開日: : FuelPHP

スクリーンショット 2012-11-11 2.12.36
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


 
 

関連記事

alexmos ジャイロ信頼度が謎

いよいよ年末が近づいてきました。年末になると色々慌ただしいというか、意味もなく忙しくなったりします。

記事を読む

【失敗】究極のプレビューモニターを発見!低価格&軽量&ワイヤレス&多機能 for SONY α and GH5

MOZA AirCrossを手に入れて、すっかり ラン&ガンスタイルに入れ込んでいます。 期待

記事を読む

ブラシレスジンバル SimpleBGC GUI 設定比較 Advanced 編

元日本代表監督の私としては、最近の戦いぶりにイマイチ納得していませんが、金曜日の夕方からサッカー日本

記事を読む

DSLR用の小さい外付けモニターで悩む

ZACUTOのゴリラプレートにアルカのプレートをつけると軸がずれてマットボックスが使えないという、な

記事を読む

FL Studio サイドチェインでワウワウ言わせる(1)

Youtubeの動画を参考にサイドチェインでベースやバッキングをワウワウ言わせてみた。   シン

記事を読む

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

Message

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


Optionally add an image (JPEG only)

Sponsored Link

エレコム GRAPH GEAR NEO カメラバッグDGB-P01BKにジンバルを入れる

やっぱり高いけどEndurance (エンデュランス) カメラバッグの

エレコム GRAPH GEAR NEO カメラバッグDGB-P01BKが北の国から

一年中GWの美少女です。 Lowepro カメラリュック フリッ

MOZA Mini-S スマホ用スタビライザーが届いた!

1万円弱で買えるスマホ用ブラシレスジンバルの新型 MOZA Mini-

GoPro Hero7 Black用 保護ケース アルミ合金保護フレームがいいよって話

別に大した話ではありません。 最近書くことが少ないのでたまには書

期待の新製品 MOZA Mini-S スマホ用スタビライザーが発売されたみたい

教えてもらった新製品のホームページで MOZA Mini-Sが紹介され

→もっと見る




  •  

PAGE TOP ↑