FuelPHP 系列(六) ------ CURD 增刪改查

1、createsql

$article = new Model_Article();
//
$article = Model_Article::forge();

// 保存數據,返回新增數據 id
$article->save();
// 有時不方便設置自增 id,可手動添加
// 添加 properties
$article = Model_Article::forge();
$article->id = 'ha123';
$article->title = 'testtitle';
$article->save();

//
$properity = array('id' => 'ha123', 'title' => 'testtitle');
$article = Model_Article::forge($properity);
$article->save();
// 框架封裝的方法不必定知足,能夠用 DB 類
$query = DB::insert('table_name', array('id', 'name'));

$res = DB::query(sql)->bind()->execute();

2、update數組

// 編輯 id=3 的數據
$article = Model_Article::find(3);
// 設置數據
$article->title = 'edit_title';
$article->author = 'test';
// 執行 update
$article->save();

//
$article = Model_Article::find(3);
$article->set(array('title' => 'edit_title', 'author' => 'test'));
$article->save();
$res = DB::update('table_name')->set(array())->execute();

$res = DB::query(sql)->bind()->execute();

3、delete緩存

// 刪除 id=3 的數據
$article = Model_Aricle::find(3);
$article->delete();
$res = DB::delete('table_name')->where()->execute();

$res = DB::query("sql")->bind()->execute();

4、read框架

// 根據 id 查
$article = Model_Article::find(2);
$article = Model_Article::find(array(2, 'foo'));

// find first/last
$entry = Model_Article::find('first');
$entry = Model_Article::find('last', array('order_by' => 'date'));

// find all
$entry = Model_Article::find('all');
$entry = Model_Article::find('all', array(
    'where' => array(
        array('category_id', 1),
    ),
    'order_by' => array('date' => 'desc'),
));
$entry = Model_Article::find('all', array(
    'where' => array(
        array('category_id', 1),
        'or' => array(
            array('category_id', 2),
        ),
    ),
));
$user = DB::select('id', 'name')->from('users')->where('id', 1)->execute();

$user = DB::select_array(array('id', 'name'))->execute();

$user = DB::query("sql")->bind()->execute();

5、其餘spa

一、sql 綁定變量:code

$title = ’iat001’;
//綁定變量
$result = DB::query("SELECT * FROM articles WHERE title = :title")->bind(’title’, $title)->execute();
//直接賦值
$result = DB::query("SELECT * FROM articles WHERE title = :title")->param(’title’, ’iat001’)->execute();
//綁定多個參數
$result = DB::query("SELECT * FROM articles WHERE title = :title AND body = :body")->parameters(array(’title’ => $title, ’body’ => ’body001’))->execute();

二、數據 cacheblog

//cached() 三個參數:緩存時間、緩存文件、是否存空值
$result = DB::query("SELECT * FROM articles")->cached(3600, "articles", false)->execute();
// 刪除 articles 緩存文件
Cache::delete("articles");
// 刪除 db 目錄下的全部緩存
Cache::delete_all("db");

三、事務事務

try {
   //事務開始      
    DB::start_transaction();
    $result = DB::insert(’articles’)->set(array(’title’ => ’iat001’, ’body’ => ’body001’))->execute();
   // 自定義返回信息
  DB::escape('ERROR:' . $msg);
   // 事務結束   
    DB::commit_transaction();
} catch(Exception $e) {
    // 事務回滾   
    DB::rollback_transaction();
   // 回滾提示信息
   $e->getMessage();
}

 四、鏈式操做get

// order_by
$query = Model_Article::query()->where('category_id', 1)->order_by('date', 'desc');
// count
$number_of_articles = $query->count();
// max
$number_of_articles = $query->max('id');
// min
$number_of_articles = $query->min('date');
// get_one
$newest_article = $query->get_one();
// limit
$all_articles = $query->limit(15)->get();
// to_array() 轉換爲數組
$entry = Model_Article::query()->where('id', '=', 8)->get_one()->to_array(true, true);

$entry = Model_Article::query()->select('name', 'date')->get();
相關文章
相關標籤/搜索