65 模型层thinkphp看书总结

1.关闭字段缓存

‘DB_FIELDS_CACHE’ => false

2.获取字段信息 主键

      $pk = $Model->getPk()

  $fields  = $User -> getDbfields ();

3.模型层可以手动定义数据表字段名称 避免IO加载开销

protected fields = array (

'id', 'username', 'passwrod', ' _pk' =>'id', '_autoinc' =>true

  );

4.跨库操作

protected 'dbName' = ' info';

protected '_tablePrefix' = '_other';

5.切换数据库

$this->db(1,"mysql://root:123456@localhost:3306/test")->query("查询SQL");

$this->db(1)->query("查询SQL");

6.分布式数据库

  1. return array(
  2.     //分布式数据库配置定义
  3.     'DB_TYPE'   => 'mysql', //分布式数据库类型必须相同
  4.     'DB_HOST'   => '192.168.0.1,192.168.0.2',
  5.     'DB_NAME'   => 'thinkphp', //如果相同可以不用定义多个
  6.     'DB_USER'   => 'user1,user2',
  7.     'DB_PWD'    => 'pwd1,pwd2',
  8.     'DB_PORT'   => '3306',
  9.     'DB_PREFIX' => 'think_',
  10.     //其他配置参数
  11.     // ...
7.读写分离
'DB_RW_SEPARATE' => true ,
8.非法html代码可以使用htmlspecialchars进行编码,以防止用户提交的html代码在展示时被执行
$_POST [ 'title' ]   =  htmlspecialchars ( $_POST [ 'title' ]);
9.查询锁定
$list  =  $User -> lock ( true )-> where ( 'status=1' )-> order ( 'create_time' )-> limit ( 10 )-> select ();
10.字段排除
$Model -> field ( 'create_time,read_count,comment_count' , true );  
11.事物支持
  1. //  在User模型中启动事务
  2. $User->startTrans();
  3.  // 进行相关的业务逻辑操作
  4. $Info = M("Info"); // 实例化Info对象
  5. $Info->save($User); // 保存用户信息
  6.  if (操作成功){
  7.     // 提交事务
  8.     $User->commit(); 
  9.  }else{
  10.    // 事务回滚
  11.    $User->rollback(); 
  12.  }

12.高级模型功能

a.字段过滤

protected $_filter = array(  

  'content'=>array('function_1','function_2'),

 )

b.延迟更新

$User->where('id=3')->setInc("score",10);// 用户的积分加10

  c.分表

  1. protected $partition = array(
  2.  'field' => 'name',// 要分表的字段 通常数据会根据某个字段的值按照规则进行分表
  3.  'type' => 'md5',// 分表的规则 包括id year mod md5 函数 和首字母
  4.  'expr' => 'name',// 分表辅助表达式 可选 配合不同的分表规则
  5.  'num' => 'name',// 分表的数目 可选 实际分表的数量
  6.  );

13.定义虚拟模型两种方式 仅用于封装业务逻辑

  a. Protected $autoCheckFields = false;

  b.Class UserModel { } //,不能再使用模型的CURD操作方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值