Model应用

模型(Model)是 MVC里的M,一般用于处理数据相关的业务逻辑。DoitPHP的Model基类提供了关系数据库(:mysql、oracle、postgresql、sqlite、mssql等)操作的类方法。下面将Model文件的应用介绍一下,前面文档:基础知识->开发规范中提到了Model文件的创建规则,在此无需赘述。创建Model文件时,建议使用DoitPHP Tools,简便快捷。举个例子:创建类 postMode.php

  1. class postModel extends Model {
  2.  
  3. }

如果所要创建的Model文件,没有数据库操作业务(无需操作数据库),则无需继承Model基类,反之则必须继承。原因很简单:Model基类中已经封装了N多种数据库操作类方法,如果不继承,怎么使用这些有用的类方法呢?Model文件如有数据库操作,一般的套路就是:一个数据表对应一个Model文件(一个Model类绑定一个数据表,对所绑定的数据表进行增、删、改、查等操作)。若有数据表操作,自然要用到数据表字段信息(获取数据表主键、数据表字段等),此时需要对自定义数据表信息的类方法进行重定义。依然使用上面postModel为例,假设postModel对应的数据表名为:posts, 其字段为 post_id,post_title,post_content,post_time,其中post_id为主键。则实例代码如下:

  1. /**
  2. * 定义数据表主键
  3. *
  4. * @access protected
  5. * @return array
  6. */
  7. protected function primaryKey() {
  8. return 'post_id';
  9. }
  10.  
  11. /**
  12. * 定义数据表字段信息
  13. *
  14. * @access protected
  15. * @return array
  16. */
  17. protected function tableFields() {
  18. return array('post_id','post_title','post_content','post_time');
  19. }
  20.  
  21. /**
  22. * 定义数据表名称
  23. *
  24. * @access protected
  25. * @return array
  26. */
  27. protected function tableName() {
  28. return 'posts';
  29. }

如果没有对上面的类方法进行重定义,Model文件的业务逻辑不会受影响,但执行数据库的写操作时,程序会自动查询数据表字段信息(第一次查询后将其字段信息生成缓存文件,下次使用时直接调用缓存),势必影响程序的执行效率。如果没有对tableName()进行重定义,在执行数据库操作时,则默认所绑定的数据表名称为当前Model文件名(如:postModel对应的数据表名为post),这样Model文件的命名就会受限。所以对上面的实例代码中的类方法进行重定义是上上策。若使用DoitPHP Tools生成Model文件时, 则自动将上面的类方法进行了重定义。继续上面的例子,postModel如何在Controller文件中调用呢?首先对model进行实例化,

  1. $postModel = $this->model('post');

如果查询post数据表的所有数据,操作如下:

  1. $data = $postModel->findAll();

如果要查看$data的数据,则:

  1. $this->dump($data);

上面的例子,用到类方法:findAll(),这就是在Model类中封装的类方法。下面将Model基类所提供的类方法及使用说明详细地讲述一下:

执行SQL语句的类方法

1、query($sql, $params = null)

|执行的SQL查询语句。本类方法需要和fetchR()或fetchAll()合用来获取数据。若想获取单行数据用fetchRow(),获取多行数据则用fetchAll()。
|参数说明:
|$sql : 所要执行的SQL语句
|$params : 待转义的数据。注:本参数支持字符串及数组

举例说明:

  1. 例一、
  2. $sql = "select * from posts post_id<15 order by post_id desc limit 0, 10";
  3. $data = $postModel->query($sql)->fetchAll();
  4. $this->dump($data);
  5.  
  6. 例二、
  7. $sql = "select * from posts where post_title=? order by post_id desc limit 0, 10";
  8. $data = $postModel->query($sql, 'doitphp')->fetchRow();
  9. $this->dump($data);
  10.  
  11. 例三、
  12. $sql = "select * from posts where post_title=? and post_time >? order by post_id desc
  13. limit 0, 10";
  14. $postModel->query($sql, array('doitphp', '2010-5-4'))->fetchAll();
  15. 或:
  16. $postModel->query($sql, 'doitphp', '2010-5-4')->fetchAll();
  17.  
  18. 例四、
  19. $sql = "show fields from posts";
  20. $data = $postModel->query($sql)->fetchAll();
  21. $this->dump($data);

2、execute($sql, $params)

|执行数据库写操作的SQL语句(无需返回信息)。如:更改、删除、添加数据信息(即:用于执行非查询SQL语句)
|参数说明:
|$sql : 所要执行的SQL语句
|$params : 待转义的数据。注:本参数支持字符串及数组

举例说明:

  1. 例一、
  2. $sql = "update posts set post_title='lucky tommy every day' where id=5";
  3. $postModel->execute($sql);
  4.  
  5. 例二、 $sql = "update posts set post_title=? where id=5";
  6. $postModel->execute($sql, 'lucky tommy every day');
  7.  
  8. 例三、
  9. $sql = "update posts set post_title=? where id=?";
  10. $postModel->execute($sql, array('lucky tommy every day', 5));
  11. 或:
  12. $postModel->execute($sql, 'lucky tommy every day', 5);
注:在使用上面这两个类方法执行sql语句时,有种情况就是:有数据表前缀时,执行的SQL语句需要怎么写?DoitPHP框架在处理这种情况时,使用一个关键字"#"。 当所执行的sql语句中含有这个关键字时,DoitPHP框架则自动将其替换为数据表前缀。如:"select * from #posts where post_id>5201314"。

主键查询

1、find($id)

|主键查询:获取一行主键查询的数据。 注:默认主键为数据表的物理主键
|参数说明:
|$id : 所要查询的主键值。注:本参数可以为数组。当为数组时,返回多行数据

举例说明:

  1. 例一、
  2. $data = $postModel->find(23);
  3. $this->dump($data);
  4.  
  5. 例二、查看主键为23的字段:post_title的数据。
  6. $data = $postModel->fields('post_title')->find(23);
  7. $this->dump($data);
  8. 注:若要实现查询数据中某字段信息,请联合下文中介绍的fields()使用。
  9.  
  10. 例三、
  11. $data = $postModel->fields(array('post_title', 'post_time'))->find(23);
  12. $data = $postModel->fields('post_title', 'post_time')->find(23);
  13. $this->dump($data);
  14.  
  15. 例四、
  16. $data = $postModel->find(array(21,23,27));
  17. $this->dump($data);
  18.  
  19. 例五、
  20. $data = $postModel->fields(array('post_id', 'post_title'))->find(array(21,23,27));
  21. $this->dump($data);

2、findAll()

|主键查询:获取数据表的全部数据信息。 以主键为中心排序,获取数据表全部数据信息。注:如果数据表数据量较大时,慎用此函数(类方法),以免数据表数据量过大,造成数据库服务器内存溢出,甚至服务器宕机
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $data = $postModel->findALL();
  3. $this->dump($data);
  4.  
  5. 例二、只查看数据表字段:post_idpost_title的数据
  6. $data = $postModel->fields('post_id', 'post_title')->findALL();
  7. $this->dump($data);
  8.  
  9. 例三、要求相比上面例二更多一些:post_id倒序排列且只显示前10条数据
  10. $data = $postModel->fields('post_id', 'post_title')->order('post_id desc')
  11. ->limit(0, 10)->findALL();
  12. $this->dump($data);

注:若实现查询数据的数据表字段筛选、排列组合等,要与下文中所讲的 fields()、order()、limit()或page_limit()等类方法联合使用。

根据条件查询

1、getOne($where = null, $value = null)

|获取查询数据的单选数据。 根据查询条件,获取单行数据,返回数据为数组型,索引为数据表字段名
|参数说明:
|$where : 查询条件
|$value : 待转义的数值,本参数支持数组。

举例说明:

  1. 例一、
  2. $data = $postModel->getOne('post_title=?', 'lucy tommy 365');
  3. $data = $postModel->where('post_title=?', 'lucy tommy 365')->getOne();
  4.  
  5. $this->dump($data);
  6.  
  7. 例二、
  8. $data = $postModel->fields('post_id')->getOne('post_title=?', 'doitphp');
  9. $data = $postModel->fields(array('post_id'))->getOne('post_title=?', 'doitphp');
  10.  
  11. $this->dump($data);
  12.  
  13. 例三、
  14. $data = $postModel->fields('post_id', 'post_title')
  15. ->getOne('post_title=? and post_id!=?', 'doitphp', 101);
  16. $data = $postModel->fields('post_id', 'post_title')
  17. ->getOne('post_title=? and post_id!=?', array('doitphp', 101));
  18.  
  19. $this->dump($data);
  20.  
  21. 例四、
  22. $data = $postModel->fields(array('post_id', ''post_title''))
  23. ->where('post_title=?', 'lucy tommy 365')->order('post_id desc')->getOne();
  24.  
  25. $this->dump($data);

2、getAll($where = null, $value = null)

|获取查询数据的全部数据。 根据查询条件,获取多行数据。并且支持数据排序,及分页的内容显示
|参数说明:
|$where : 查询条件
|$value : 待转义的数值,本参数支持数组。

举例说明:

  1. 例一、
  2. $data = $postModel->getAll('post_title=?', 'lucy tommy 365');
  3. $data = $postModel->where('post_title=?', 'lucy tommy 365')->getAll();
  4.  
  5. $this->dump($data);
  6.  
  7. 例二、
  8. $data = $postModel->getAll('post_title=? and post_id!=?', 'lucy tommy 365', 101);
  9. $data = $postModel->getAll('post_title=? and post_id!=?', array('lucy tommy 365', 101));
  10.  
  11. $this->dump($data);
  12.  
  13. 例三、查询数据中 数据表字段的筛选
  14. $data = $postModel->fields('post_id')->getAll('post_title=?', 'doitphp');
  15. $data = $postModel->fields(array('post_id'))->getAll('post_title=?', 'doitphp');
  16.  
  17. $this->dump($data);
  18.  
  19. 例四、多条件查询
  20. $data = $postModel->fields(array('post_id', ''post_title''))
  21. ->where('post_title=?', 'lucy tommy 365')->order('post_id desc')
  22. ->limit(0, 10)->getAll();
  23.  
  24. $this->dump($data);

数据表的UID操作(增加、删除、更改)

1、insert($data, $isReturnId = false)

|数据表写入操作
|参数说明:
|$data : 所要写入的数据内容。注:数据必须为数组
|$isReturnId : 是否返回数据为:last insert id

举例说明:

  1. 例一、
  2. $insertArray = array('post_title'=>'good luck every day', 'post_content'=>'略150字');
  3. $postModel->insert($insertArray);
  4.  
  5. 例二、
  6. $insertArray = array('post_title'=>'返回insert id', 'post_content'=>'省略350字');
  7. $postModel->insert($insertArray, true);

2、replace($data)

|数据表数据替换操作
|参数说明:
|$data : 所要替换的数据内容。注:数据必须为数组

举例说明:

  1. 例一、
  2. $replaceArray = array('post_id'=>5, 'post_title'=>'新年快乐', 'post_content'=>'略');
  3. $postModel->replace($replaceArray);

3、update($data, $where = null, $value = null)

|数据表更新操作
|参数说明:
|$data : 所要更改的数据内容
|$where : 更改数据所须的条件
|$value : 待转义的参数值

举例说明:

  1. 例一、
  2. $updateArray = array('post_title'=>'恭喜发财', 'post_content'=>'省略150字');
  3. $postModel->update($updateArray, 'post_id=2016');
  4.  
  5. 例二、
  6. $updateArray = array('post_title'=>'恭喜发财', 'post_content'=>'省略150字');
  7. $postModel->update($updateArray, 'post_tile=?', '圣诞快乐');
  8.  
  9. 例三、
  10. $updateArray = array('post_title'=>'恭喜发财', 'post_content'=>'省略150字');
  11. $postModel->update($updateArray, 'post_tile=? and post_id>?', array('圣诞快乐', 175));
  12. $postModel->update($updateArray, 'post_tile=? and post_id>?', '圣诞快乐', 175);

4、delete($where = null, $value = null)

|数据表删除操作
|参数说明:
|$where : 删除数据所需的SQL条件
|$value : 待转义的参数值

举例说明:

  1. 例一、
  2. $postModel->delete('post_id=2016');
  3.  
  4. 例二、
  5. $postModel->delete('post_title=?', '欢庆2016新年');
  6.  
  7. 例三、
  8. $postModel->delete('post_title=? or post_title=?', array('欢庆2016新年', '新春快乐'));
  9. $postModel->delete('post_title=? or post_title=?', '欢庆2016新年', '新春快乐');

组装SQL语句

1、where($where, $value = null)

|组装SQL语句的WHERE语句。 用于getOne()、getAll()等类方法的条件查询
|参数说明:
|$where : Where的条件
|$value : 待转义的数值

举例说明:

  1. 例一、
  2. $postModel->where('post_title=?', '2017年')->getAll();
  3.  
  4. 例二、
  5. $postModel->where('post_id!=? and post_title=?', '44', '恭喜发财')->getAll();
  6.  
  7. 例三、
  8. $postModel->where('post_title=?', '欢庆2016新年')->delete();

2、order($orderDesc)

|组装SQL语句排序(ORDER BY)语句。 用于getAll()的数据排行
|参数说明:
|$orderDesc : 排序条件。注:本参数支持数组

举例说明:

  1. 例一、
  2. $postModel->where('post_title=?', '2017年')->where('post_id>2016')
  3. ->order('post_id desc')->getAll();

3、fields($fieldName)

|组装SQL语句的查询字段
|参数说明:
|$fieldName : 所要查询的数据表字段信息

举例说明:

  1. 例一、
  2. $postModel->fields(array('post_id', 'post_title'))
  3. ->where('post_title=?', '2017年')->getAll();
  4.  
  5. 例二、
  6. $postModel->fields('post_id', 'post_title')
  7. ->where('post_title=?', '2017年')->getAll();

4、limit($limitStart, $listNum = null)

|组装SQL语句LIMIT语句。 limit(10,20)用于处理LIMIT 10, 20之类的SQL语句部分
|参数说明:
|$limitStart : 启始id。注:参数为整形
|$listNum : 显示的行数

举例说明:

  1. 例一、
  2. $postModel->where('post_id>2016')->limit(0, 20)->getAll();

5、pageLimit($page, $listNum)

|组装SQL语句的LIMIT语句。 注:本方法与$this->limit()功能相类,区别在于:本方法便于分页,参数不同
|参数说明:
|$page : 当前的页数
|$listNum : 每页显示的数据行数

举例说明:

  1. 例一、
  2. $postModel->where('post_id>2016')->pageLimit(5, 10)->getAll();
以上这些所谓的SQL组装类方法并非组装完整的SQL语句,仅供getOne()、getAll()、find()、findAll()、update()、delete()以及下文中的count()、max()、min()、distinct(),sum()、avg()这些类方法调用。

常用的数据库函数操作

1、count($fieldName = null, $where = null, $value = null)

|获取查询信息的数据总行数
|参数说明:
|$fieldName : 所要查询字段名称
|$where : 查询条件
|$value : 数值

举例说明:

  1. 例一、
  2. $postModel->count('post_id', 'post_title=?', '2017年');
  3. $postModel->fields('post_id')->where('post_title=?', '2017年')->count();

2、distinct($fieldName = null, $where = null, $value = null)

|获取查询信息的某数据表字段的唯一值的数据
|参数说明:
|$fieldName : 所要查询字段名称
|$where : 查询条件
|$value : 数值

举例说明:

  1. 例一、
  2. $postModel->distinct('post_title', 'post_title=?', '2017年');
  3. $postModel->fields('post_title')->where('post_title=?', '2017年')->distinct();

3、max($fieldName = null, $where = null, $value = null)

|获取查询信息某数据表字段的最大值
|参数说明:
|$fieldName : 所要查询字段名称
|$where : 查询条件
|$value : 数值

举例说明:

  1. 例一、
  2. $postModel->max('post_id', 'post_id>2016');
  3. $postModel->fields('post_id')->where('post_id>2016')->max();

4、min($fieldName = null, $where = null, $value = null)

|获取查询信息某数据表字段的最小值
|参数说明:
|$fieldName : 所要查询字段名称
|$where : 查询条件
|$value : 数值

举例说明:

  1. 例一、
  2. $postModel->min('post_id', 'post_id<2017');
  3. $postModel->fields('post_id')->where('post_id<2017')->min();

5、sum($fieldName = null, $where = null, $value = null)

|获取查询信息某数据表字段的数据和
|参数说明:
|$fieldName : 所要查询字段名称
|$where : 查询条件
|$value : 数值

举例说明:

  1. 例一、
  2. $postModel->sum('post_id', 'post_id<100');
  3. $postModel->fields('post_id')->where('post_id<100')->sum();

6、avg($fieldName = null, $where = null, $value = null)

|获取查询信息某数据表字段的数据的平均值
|参数说明:
|$fieldName : 所要查询字段名称
|$where : 查询条件
|$value : 数值

举例说明:

  1. 例一、
  2. $postModel->avg('post_id', 'post_id<100');
  3. $postModel->fields('post_id')->where('post_id<100')->avg();
  4.  
  5. 例二、
  6. $postModel->avg('post_id', 'post_id>? and post_id<?', array(200, 1000));
  7. $postModel->avg('post_id', 'post_id>? and post_id<?', 200, 1000);
  8.  
  9. 注:例二中 当待转义的变量有多个时,参数$value可以使用数组,也可以将多个变量,从$value参数开始,
  10. 依次作为参数。上面的count()、distinct()、max()、min()、sum()这些类方法的参数$value同样
  11. 也支持这样使用。

事务处理操作

1、startTrans()

|事务处理:开启事务处理
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $postModel->startTrans();

2、commit()

|事务处理:提交事务处理
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $postModel->commit();

3、rollback()

|事务处理:事务回滚
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $postModel->rollback();

其它

1、getLastInsertId()

|获取数据表写入时的最新的Insert Id。通常用于前文insert()操作后,获取最新id。
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $insertArray = array('post_title'=>'略', 'post_content'=>'省略60字');
  3. $postModel->insert($insertArray);
  4. $postModel->getLastInsertId();

2、quoteInto($value)

|字符串转义函数。SQL语句指令安全过滤,用于字符转义。一般情况下很少用到。
|参数说明:
|$value : 所要转义的字符或字符串。注:参数支持数组

举例说明:

  1. 例一、
  2. $value = $postModel->quoteInto('年味浓');
  3. $postModel->where('post_title=?', $value)->getAll();

3、getConnection($adapter)

|获取当前数据库连接的实例化对象。 使用类方法,可以实现对原生PDO所提供的函数的调用
|参数说明:
|$adapter : 是否为主数据库。true:主数据库/false:从数据库

举例说明:

  1. 例一、
  2. $dbConnection = $postModel->getConnection();
  3. $dbConnection->getLastError();

4、createCommand()

|创建SQL查询语句组装实例化对象
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $sqlObj = $postModel->getConnection();
  3. $list = $sqlObj->from('posts', 'post_title')->where('post_id<2017')
  4. ->query()->fetchAll();
  5. $this->dump($list);

5、setErrorInfo($message)

|设置错误信息。只限Model文件中使用!
|参数说明:
|$message: 错误信息

使用举例:

  1. 例一、
  2. $this->setErrorInfo('所添加的数据已存在');

6、getErrorInfo()

|获取当前模型的错误信息
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $errorInfo = $postModel->getErrorInfo();
  3.  
  4. $this->ajax(false, $errorInfo);

7、getTableName()

|获取当前模型(Model)文件所对应的数据表的名称。 注:若数据表有前缀($prefix)时,系统将自动加上数据表前
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $sql = "select * form " . $postModel->getTableName() . " where post_id=2017";
  3. $postModel->query($sql)->fetchRow();

8、setTableName($tableName)

|设置当前模型(Model)文件所对应的数据表的名称。 注:数据表名称不含数据表前缀($prefix)
|参数说明:
|$tableName : 数据表名称

举例说明:

  1. 例一、
  2. $model = Model::getInstance();
  3.  
  4. $model->setTableName('posts');
  5. $data = $model->findAll();
  6.  
  7. $this->dump($data);

9、getTablePrefix()

|获取当前模型(Model)文件所对应的数据表前
|参数说明:
|参数为空

举例说明:

  1. 例一、
  2. $sql = "select * form " . $postModel->getTablePrefix() . "posts where post_id=2017";
  3. $postModel->query($sql)->fetchRow();

10、model($modelName)

|实例化模型类。专用于在model文件中,用来自定义业务逻辑时,实例化其它的模型类。
|参数说明:
|$modelName : 所要实例化的模型类的名称

11、getConfig($fileName)

|静态获取配置文件的内容,专用于在model文件中。 注:此配置文件非数据库连接配置文件,而是其它用途的配置文件。详细信息参见Controller Class中的类方法getConfig()
|参数说明:
|$fileName : 配置文件的名称。注:不含有“.php”后缀

12、dump($data, $type)

|调试类方法:优雅输出print_r()函数所要输出的内容,专用于在model文件中。 注:详细信息参见Controller Class中的类方法dump()
|参数说明:
|$data : 所要输出的数据
|$type : 输出的信息是否含有数据类型信息。true:支持/false:不支持

13、removeCache($tableName)

|删除当前模型(Model)文件的缓存文件。 注:如果自定了数据表的字段信息及主键信息,则当前模型(Model)文件的缓存文件不再被使用。一般没有卵用,但又不得不介绍。
|参数说明:
|$tableName : 数据表名

举例说明:

  1. 例一、
  2. $postModel->removeCache('posts');

原文: http://www.doitphp.com/index/documentation/?articleid=20