在之前的博客中,我们实现并完善了Model类的findOne方法,下面我们来实现其中的其他方法。

先来看findAll方法,这个方法和findOne很相似。

    public static function findOne($condition = null)
{
$sql = 'select * from ' . static::tableName();
$params = []; // 判空
if (!empty($condition)) {
$sql .= ' where ';
$params = array_values($condition);
$keys = [];
foreach ($condition as $key => $value) {
array_push($keys, "$key = ?");
}
$sql .= implode(' and ', $keys);
} $stmt = static::getDb()->prepare($sql);
$rs = $stmt->execute($params);
$models = []; if ($rs) {
// 直接获取出所有符合条件的
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
if (!empty($row)) {
$model = new static();
foreach ($row as $rowKey => $rowValue) {
$model->$rowKey = $rowValue;
}
array_push($models, $model);
}
}
} return null;
}

你会发现有findOne和findAll方法很相似,明显可以将公共的部分抽出来,然后我们就多了如下两个方法:

    /**
* Build a sql where part
* @param mixed $condition a set of column values
* @return string
*/
public static function buildWhere($condition, $params = null)
{
if (is_null($params)) {
$params = [];
} $where = '';
if (!empty($condition)) {
$where .= ' where ';
$keys = [];
foreach ($condition as $key => $value) {
array_push($keys, "$key = ?");
array_push($params, $value);
}
$where .= implode(' and ', $keys);
}
return [$where, $params];
} /**
* Convert array to model
* @param mixed $row the row data from database
*/
public static function arr2Model($row)
{
$model = new static();
foreach ($row as $rowKey => $rowValue) {
$model->$rowKey = $rowValue;
}
return $model;
}

分别是构建sql中where部分的方法和将查找到的Array转换成Model的方法。大家会奇怪第一个方法中为什么需要params参数和返回值,其实这个为了之后的updateAll方法的使用。其实这个地方跟适合使用引用传值。

这样我们的findOne和findAll就便成了如下内容:

    /**
* Returns a single model instance by a primary key or an array of column values.
*
* ```php
* // find the first customer whose age is 30 and whose status is 1
* $customer = Customer::findOne(['age' => 30, 'status' => 1]);
* ```
*
* @param mixed $condition a set of column values
* @return static|null Model instance matching the condition, or null if nothing matches.
*/
public static function findOne($condition = null)
{
list($where, $params) = static::buildWhere($condition);
$sql = 'select * from ' . static::tableName() . $where; $stmt = static::getDb()->prepare($sql);
$rs = $stmt->execute($params); if ($rs) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!empty($row)) {
return static::arr2Model($row);
}
} return null;
} /**
* Returns a list of models that match the specified primary key value(s) or a set of column values.
*
* ```php
* // find customers whose age is 30 and whose status is 1
* $customers = Customer::findAll(['age' => 30, 'status' => 1]);
* ```
*
* @param mixed $condition a set of column values
* @return array an array of Model instance, or an empty array if nothing matches.
*/
public static function findAll($condition = null)
{
list($where, $params) = static::buildWhere($condition);
$sql = 'select * from ' . static::tableName() . $where; $stmt = static::getDb()->prepare($sql);
$rs = $stmt->execute($params);
$models = []; if ($rs) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
if (!empty($row)) {
$model = static::arr2Model($row);
array_push($models, $model);
}
}
} return $models;
}

剩下的updateAll/deleteAll/insert/update和delete方法就不一一详细说明了,直接给出代码。其基本思想都是一致的,都是按照规则拼接SQL语句。

    /**
* Updates models using the provided attribute values and conditions.
* For example, to change the status to be 2 for all customers whose status is 1:
*
* ~~~
* Customer::updateAll(['status' => 1], ['status' => '2']);
* ~~~
*
* @param array $attributes attribute values (name-value pairs) to be saved for the model.
* @param array $condition the condition that matches the models that should get updated.
* An empty condition will match all models.
* @return integer the number of rows updated
*/
public static function updateAll($condition, $attributes)
{
$sql = 'update ' . static::tableName();
$params = []; if (!empty($attributes)) {
$sql .= ' set ';
$params = array_values($attributes);
$keys = [];
foreach ($attributes as $key => $value) {
array_push($keys, "$key = ?");
}
$sql .= implode(' , ', $keys);
} list($where, $params) = static::buildWhere($condition, $params);
$sql .= $where; $stmt = static::getDb()->prepare($sql);
$execResult = $stmt->execute($params);
if ($execResult) {
// 获取更新的行数
$execResult = $stmt->rowCount();
}
return $execResult;
} /**
* Deletes models using the provided conditions.
* WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
*
* For example, to delete all customers whose status is 3:
*
* ~~~
* Customer::deleteAll([status = 3]);
* ~~~
*
* @param array $condition the condition that matches the models that should get deleted.
* An empty condition will match all models.
* @return integer the number of rows deleted
*/
public static function deleteAll($condition)
{
list($where, $params) = static::buildWhere($condition);
$sql = 'delete from ' . static::tableName() . $where; $stmt = static::getDb()->prepare($sql);
$execResult = $stmt->execute($params);
if ($execResult) {
// 获取删除的行数
$execResult = $stmt->rowCount();
}
return $execResult;
} /**
* Inserts the model into the database using the attribute values of this record.
*
* Usage example:
*
* ```php
* $customer = new Customer;
* $customer->name = $name;
* $customer->email = $email;
* $customer->insert();
* ```
*
* @return boolean whether the model is inserted successfully.
*/
public function insert()
{
$sql = 'insert into ' . static::tableName();
$params = [];
$keys = [];
foreach ($this as $key => $value) {
array_push($keys, $key);
array_push($params, $value);
}
// 构建由?组成的数组,其个数与参数相等数相同
$holders = array_fill(0, count($keys), '?');
$sql .= ' (' . implode(' , ', $keys) . ') values ( ' . implode(' , ', $holders) . ')'; $stmt = static::getDb()->prepare($sql);
$execResult = $stmt->execute($params);
// 将一些自增值赋回Model中
$primaryKeys = static::primaryKey();
foreach ($primaryKeys as $name) {
// Get the primary key
$lastId = static::getDb()->lastInsertId($name);
$this->$name = (int) $lastId;
}
return $execResult;
} /**
* Saves the changes to this model into the database.
*
* Usage example:
*
* ```php
* $customer = Customer::findOne(['id' => $id]);
* $customer->name = $name;
* $customer->email = $email;
* $customer->update();
* ```
*
* @return integer|boolean the number of rows affected.
* Note that it is possible that the number of rows affected is 0, even though the
* update execution is successful.
*/
public function update()
{
$primaryKeys = static::primaryKey();
$condition = [];
foreach ($primaryKeys as $name) {
$condition[$name] = isset($this->$name) ? $this->$name : null;
} $attributes = [];
foreach ($this as $key => $value) {
if (!in_array($key, $primaryKeys, true)) {
$attributes[$key] = $value;
}
} return static::updateAll($condition, $attributes) !== false;
} /**
* Deletes the model from the database.
*
* @return integer|boolean the number of rows deleted.
* Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.
*/
public function delete()
{
$primaryKeys = static::primaryKey();
$condition = [];
foreach ($primaryKeys as $name) {
$condition[$name] = isset($this->$name) ? $this->$name : null;
} return static::deleteAll($condition) !== false;
}

这样基本的Model就算是暂时完成了,虽然可能还有很多问题和局限,但暂时先这样了,我们之后有机会会一步一步的去完善。

好了,今天就先到这里。项目内容和博客内容也都会放到Github上,欢迎大家提建议。

code:https://github.com/CraryPrimitiveMan/simple-framework/tree/0.7

blog project:https://github.com/CraryPrimitiveMan/create-your-own-php-framework

构建自己的PHP框架--实现Model类(3)的更多相关文章

  1. 构建自己的PHP框架--实现Model类(1)

    在之前的博客中,我们定义了ORM的接口,以及决定了使用PDO去实现.最后我们提到会有一个Model类实现ModelInterface接口. 现在我们来实现这个接口,如下: <?php names ...

  2. 构建自己的PHP框架--实现Model类(2)

    在上一篇博客中我们简单实现了findOne方法,但我们可以看到,还是有一些问题的,下面我们来修正一下这些问题. 首先是返回的数据中,数字被转换成了字符串.我们需要的是数字啊... PDO中有属性可以支 ...

  3. tp框架之Model类与命名空间

    1.获取系统常量信息 public function shuchu() { var_dump(get_defined_constants()); } 2.跨控制器或跨模块调用 function dia ...

  4. 为测试框架model类自动生成xml结果集

    问题:有大量类似于theProductId这样名字的字符串需要转换成the_product_id这种数据库column名的形式. 思路:见到(见)大写字母(缝)就插入(插)一个“_”字符(针)进去,最 ...

  5. J2EE进阶(七)利用SSH框架根据数据表建立model类

    J2EE进阶(七)利用SSH框架根据数据表建立model类 前言 在利用SSH框架进行项目开发时,若将数据库已经建好,并且数据表之间的依赖关系已经确定,可以利用Hibernate的反转功能进行mode ...

  6. laravel5.1框架model类查询实现

    laravel框架model类查询实现: User::where(['uid'=8])->get(); User类继承自Model类:Illuminate\Database\Eloquent\M ...

  7. yii框架之gii创建数据表相应的model类

    一.首先是在数据库中建立project须要的表: 二.然后,配置相应文件: 在project文件夹下yiiProject\protected\config\main.php.在50行定义了db应用组件 ...

  8. 如何构建Android MVVM 应用框架

    概述 说到Android MVVM,相信大家都会想到Google 2015年推出的DataBinding框架.然而两者的概念是不一样的,不能混为一谈.MVVM是一种架构模式,而DataBinding是 ...

  9. 构建自己的PHP框架--创建组件的机制

    在之前的博客中,我们完成了基本的Model类,但是大家应该还记得,我们创建数据库的pdo实例时,是hard好的配置,并且直接hard在Model类中. 代码如下: public static func ...

随机推荐

  1. 在linux下Ant的环境配置

    Ant(英文全称为another neat tool,另一个简洁的工具)是一个基于Java的生成工具,Ant将会被应用到Java项目中. 同样的,现在要来安装Ant(最近要安装的东西还蛮多的=m=), ...

  2. php随机ip

    $ip_long = array( array('607649792', '608174079'), //36.56.0.0-36.63.255.255 array('1038614528', '10 ...

  3. spring+mybatis+oracle/mysql整合开发需要的jar包详解

    导入spring,mybatis,c3p0,oracle和mybatis提供的与spring整合的插件包   mysql的jar:         mysql-connector-java-5.1.7 ...

  4. CharSequence cannot be resolved. It is indirectly referenced from required .class files

    最近在写项目的时候发现会莫名其妙的出现这个编译错误,网上查了下发现自己安装的是JDK1.8造成的 原因:JDK1.8版本现在还不稳定 解决方法:卸载JDK1.8,安装JDK1.7 扩展:发现安装JDK ...

  5. 第三方框架之ThinkAndroid 学习总结(一)

    ThinkAndroid是一个免费的开源的.简易的.遵循Apache2开源协议发布的Android开发框架,其开发宗旨是简单.快速的进行Android应用程序的开发,包含Android mvc.简易s ...

  6. angularjs服务-service

    Service 的特性 ①service都是单例的 ②service由$injector 负责实例化 ③service在整个应用的声明周期中存在,可以用来共享数据 ④在需要使用的地方利用依赖注入ser ...

  7. 初探ReactJS.NET 开发

    ReactJS通常也被称为"React",是一个刚刚在这场游戏中登场的新手.它由Facebook创建,并在2013年首次发布.Facebook认为React在处理SPA问题上可以成 ...

  8. 关于EF6的记录Sql语句 与 EntityFramework.Extend 的诟病

    1.关于EF6的记录Sql语句,一个老生长谈的问题. 他生成的sql语句实在是烂,大家都这样说 2.EF6 更新删除不方便,没有批量操作.所以,有人出了EF6.Extend  大家用起来也很爽 基于以 ...

  9. 给Macbook Pro更换固态硬盘并转移系统的最简单办法

    没有固态硬盘,再快的CPU,再强悍的显卡,都是白搭,由于“木桶原理”,瓶颈就在这里啊.如今的固态硬盘价格跌了很多,我记得去年我买的120G的固态硬盘还要将近600元,而现在只需要不到400了.   我 ...

  10. SQL Server null知多少?

    null是什么? 不知道.我是说,他的意思就是不知道(unknown). 它和true.false组成谓词的三个逻辑值,代表“未知”.与true和false相比,null最难以令人捉摸,因为它没有明确 ...