ThinkPHP数据库操作

$pagemodel = D(“Page”);

$result = $pagemodel->findAll();
   // ...->$pagemodel->find("id='".$_GET[id]."'");
dump($result);

视图查询   在Lib\Model目录下编写PageViewModel.class.php如下
<?php
class PageViewModel extends Model{
protected $viewModel = true;
var $masterModel = 'Page';
// 定义视图中的字段
protected $viewFields = array(         'Page'=>array('id','title','category_id','content','user_id','addtime','lastmodify','pid'),
   'User'=>array('loginid')
   );
// 定义基础查询条件
protected $viewCondition = array("Page.user_id" => array('eqf',"User.id"));
// 定义视图主键名称
Public function getPk() {
   return 'id';
}
}
?>

ThinkPHP实现了ActiveRecords模式的ORM模型,即
表映射到类,记录(集)映射到对象,
字段属性映射到对象的虚拟属性

... extends Model{}内
1.find,save,add,delete()
$User= D("User");
//相当于 $User= new UserModel(); 实例化User对象
$User->find(1); //查找主键为1的记录
$User->name='ThinkPHP'; //...     //查找到的记录字段名称改为ThinkPHP
$User->save(); //save()改delete(),deleteAll()
//若不存在find(),新建对象后,给数据对象赋值
//即新建后,直接到$User->name='ThinkPHP'; //...
//$User->add()

2.create
$User=D("User");
$User->create(); //默认通过表单提交的数据进行创建
$User->add(); //新增
*******************************
$data['name']='ThinkPHP'; //...
$User->create($data);
$Member=D("Member");
$Member->create($User);

3.将find()改为findAll('1,3,8'),再用foreach的格式dump

4.条件查询
$map->put('name','php'); //name='php'
..('name',array('like','think')); //name like '...'
..('id',array('in',array(1,2,4)));
..('id',array('10','3','or')); //id>=10 or <=3
//分类,再用array

5.原始法
$Model= new Model()       
$Model->query("select * from...");
//update和insert,要改query为execute
affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Comments are closed.