FuelPHPでmodel呼び出しを行う
FuelPHPにて、いくつかのパターンでモデルを呼び出してみます。
!!! 【注意】model名は小文字で。命名規則があるので注意しましょう !!!
namespaceを使用したModel呼び出し その1
公式サイト
http://press.nekoget.com/fuelphp_doc/general/models.html
にある呼び出し
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | vi fuel/app/classes/model/contents.php   <?php namespace Model;   class Contents extends Model {       public static function get_list($date, $limit=5, $offset=0)     {         $ret = DB::select()->                 from('contents')->                 where('published', '>=', $date->format('Y-m-d H:i:s'))->                 order_by('published', 'desc')->                 limit($limit)->                 offset($offset)->                 execute();           return $ret;     }   }   | 
				
			 
		 
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | vi fuel/app/classes/controller/welcome.php     <?php   use ModelContents;   class Controller_Welcome extends Controller {       public function action_index()     {           $date = new DateTime();         $date->modify('-1 days');           $query = Contents::get_list( $date, 10, 0);         print_r($query->as_array());           return Response::forge(View::forge('welcome/index'));     }     | 
				
			 
		 
namespaceを使用したModel呼び出し その2
コントローラーからの呼び出し方法を変えました。
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | vi fuel/app/classes/model/contents.php   <?php namespace Model;   class Contents extends Model {       public static function get_list($date, $limit=5, $offset=0)     {         $ret = DB::select()->                 from('contents')->                 where('published', '>=', $date->format('Y-m-d H:i:s'))->                 order_by('published', 'desc')->                 limit($limit)->                 offset($offset)->                 execute();           return $ret;     }   }   | 
				
			 
		 
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | vi fuel/app/classes/controller/welcome.php   <?php   //use ModelContents;   class Controller_Welcome extends Controller {       public function action_index()     {           $date = new DateTime();         $date->modify('-1 days');   //        $query = Contents::get_list( $date, 10, 0);         $query = ModelContents::get_list($date, 10, 0);           print_r($query->as_array());             return Response::forge(View::forge('welcome/index'));     }     | 
				
			 
		 
namespaceを使用しないModel呼び出し
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | vi fuel/app/classes/model/contents.php     <?php   class Model_Contents extends Model {       public static function get_list($date, $limit=5, $offset=0)     {         $ret = DB::select()->                     from('contents')->                     where('published', '>=', $date->format('Y-m-d H:i:s'))->                     order_by('published', 'desc')->                     limit($limit)->                     offset($offset)->execute();           return $ret;     } }     | 
				
			 
		 
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | vi fuel/app/classes/controller/welcome.php     <?php   //use ModelContents;   class Controller_Welcome extends Controller {       public function action_index()     {           $date = new DateTime();         $date->modify('-1 days');   //        $query = Contents::get_list( $date, 10, 0); //        $query = ModelContents::get_list($date, 10, 0);         $query = Model_Contents::get_list($date, 10, 0);           print_r($query->as_array());           return Response::forge(View::forge('welcome/index'));     }     | 
				
			 
		 
おまけ
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | vi fuel/app/classes/controller/welcome.php     <?php   //use ModelContents;   class Controller_Welcome extends Controller {       public function action_index()     {           $date = new DateTime();         $date->modify('-1 days');   //        $query = Contents::get_list( $date, 10, 0); //        $query = ModelContents::get_list($date, 10, 0); //        $query = Model_Contents::get_list($date, 10, 0);             $query = DB::select()->                     from('contents')->                     where('published', '>=', $date->format('Y-m-d H:i:s'))->                     order_by('published', 'desc')->                     limit(10)->                     offset(0)->execute();           print_r($query->as_array());             return Response::forge(View::forge('welcome/index'));     }     | 
				
			 
		 
今回はDBクラスのクエリビルダを使用したサンプルでした。
オブジェクトリレーショナルマッパー(ORM)を使用してデータベースにアクセスすることも可能です。
		
		
			
			
				
					
				| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 1.configのpackagesを 'packages' => array(  'orm', ),     2.ormを継承したクラスを作成 class Model_Article extends OrmModel{ ... DBのフィールド名や、テーブル名、プライマリーキーを記述   }   3.呼び出し // find all articles $entry = Model_Article::find_all();   // find all articles from category 1 order descending by date $entry = Model_Article::find(array(     'where' => array('category_id', 1),     'order_by' => array('date' => 'desc') ));   |