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

一、create

$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();

二、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();

三、delete

// 删除 id=3 的数据
$article = Model_Aricle::find(3);
$article->delete();
$res = DB::delete('table_name')->where()->execute();

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

四、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();

五、其他

1、sql 绑定变量:

$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();

2、数据 cache

//cached() 三个参数:缓存时间、缓存文件、是否存空值
$result = DB::query("SELECT * FROM articles")->cached(3600, "articles", false)->execute();
// 删除 articles 缓存文件
Cache::delete("articles");
// 删除 db 目录下的所有缓存
Cache::delete_all("db");

3、事务

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();
}

 4、链式操作

// 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();

 

posted @ 2018-07-26 16:43  菜乌  阅读(810)  评论(0编辑  收藏  举报