*

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


 
 

関連記事

VirtualBox FL Studio で microKEY-25BKRDを動かす

microKEY-25BKRDが届いたので開封画像を書いてみる。いつもの通り速攻でアマゾンから到着。

記事を読む

キヤノン LP-E6 互換バッテリー をCL5585H用に購入

Panasonic GH4 の登場で、購入計画を妄想している時に不意打ちを食らわしてくれた Blac

記事を読む

no image

韓国はアホだが日本は間抜けじゃないのか?

前回の続き だいたい国際関係が熱くなってくると、日本のメディアや政治家に「冷静に」とか「品位が」とか

記事を読む

ブラシレスジンバル:2.42b7と上下動を抑えた歩き方の実験

とりあえずなのかデザインはともかく大注目の製品が間もなく登場するようです。予想通りの製品であれば、今

記事を読む

やっと来た Godox Professionl Video Light White Bulbs (5600K) LED500W

少々 rig関係で煮詰まっているので気分転換に LEDライトの続きを。 過去記事「到着したのは

記事を読む

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

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が紹介され

→もっと見る




  •  

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

PAGE TOP ↑