facebook twitter hatena line email

Php/fuelphp/db orm

提供: 初心者エンジニアの簡易メモ
2016年2月7日 (日) 16:31時点におけるAdmin (トーク | 投稿記録)による版

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

crudとは

「作成(Create)」「読み出し(Read)」「更新(Update)」「削除(Delete)」をそれぞれ頭文字で表したものでクラッドと読む

必須設定

  1. ormパッケージを使用可能に設定する

以下箇所のコメントアウトを取り除く

fuel/app/config/config.php

'always_load'  => array(
        'packages'  => array(
            'orm',
         ),
),

以下ファイルを作成

classes/model/test.php

class Model_Test extends Orm\Model
{
    protected static $_properties = array(
        'id',
        'name',
        'created_at',
        'updated_at'
    );
    protected static $_observers = array(
        'Orm\Observer_CreatedAt' => array(
            'events' => array('before_insert'),
            'mysql_timestamp' => false,
        ),
        'Orm\Observer_UpdatedAt' => array(
            'events' => array('before_save'),
            'mysql_timestamp' => false,
        ),
    );
}

データ取得

$test = Model_Test::find(1);
echo $test->name; // taro
var_dump($test->to_array()); // 連想配列で取得

データ取得(最初の1行

$test = Model_Test::first();
echo $test->name; // taro
$test = Model_Test::find('first', array('where' => array('name' => 'taro')));
print $test->name; // taro

データ全部取得

$tests = Model_Test::find('all'); // 連想配列の中にモデルクラスが格納されている
foreach ($tests as $test) {
  echo $test->name; // taro
}

データ条件取得

$tests = Model_Test::find('all', array(
   'where' => array(
       array('category_id', 1),
       ),
   ),
));
$tests = Model_Test::find('all', array(
   'where' => array(
       array('category_id', 1),
       'or' => array(
           array('category_id', 2),
       ),
   ),
));

データ条件&limit付き

$tests = Model_Test::find('all', array('limit' => 9
       ,'order_by' => array('name')));

データ条件順序指定

'order_by' => array('date' => 'desc'),

データ条件順序複数指定

'order_by' => array(array('name' => 'desc', 'category_id' => 'asc')),

件数取得

$cnt = Model_test::count();

最大最小取得

$cnt = Model_test::max('column1');
$cnt = Model_test::min('column1');

データ挿入

$test = Model_Test::forge(array(
    'name' => 'taro',
));
if ($test and $test->save()) {

データ更新

$test = Model_Test::find(1);
$test->name = 'taro';
if ($test and $test->save()) {

データ削除

if ($test = Model_Test::find(1))
{
      $test->delete();

like検索

Model_Test::find('all', array(
'where' => array(array('name', 'LIKE', '%'.$keyword.'%')),
'limit' => 100,
));

カプセル化

こんな感じでまとめる

   public static function findRowByUrl($url)
   {
       return self::find('first', array('where' => array('url' => $url)));
   }
   public static function replaceByUrl($data, $url)
   {
       $row = self::findRowByUrl($url);
       // update
       if ($row) {
           $row->rank = $data['rank'];
           $row->updated_at = time(); // dbが変更されないとupdated_atが更新されないので
           if ($row and $row->save()) {
           }
       // insert
       } else {
           $row = self::forge($data);
           if ($row and $row->save()) {
           }
       }
   }

注意

fuelphp1.1からシンプルなModel_Crudが使えるようになったので混同しないように。

modelディレクトリの下の階層にモデルクラスを作る場合のクラス名

$data['project_hoge'] = Model_Project_Hoge::find($id);

vi model/project/hoge.php
class Model_Project_Hoge extends \Orm\Model
{
  protected static $_properties = array(
  以下略

参考

公式fuelphp http://fuelphp.jp/docs/1.6/packages/orm/crud.html