在之前的博客中,我们定义了ORM的接口,以及决定了使用PDO去实现。最后我们提到会有一个Model类实现ModelInterface接口。

现在我们来实现这个接口,如下:

<?php
namespace sf\db; use PDO; /**
* Model is the base class for data models.
* @author Harry Sun <sunguangjun@126.com>
*/
class Model implements ModelInterface
{
/**
* Declares the name of the database table associated with this Model class.
* @return string the table name
*/
public static function tableName()
{
return get_called_class();
} /**
* Returns the primary key **name(s)** for this Model class.
* @return string[] the primary key name(s) for this Model class.
*/
public static function primaryKey()
{
return ['id'];
} /**
* 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)
{ } /**
* 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)
{ } /**
* 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)
{ } /**
* 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)
{ } /**
* 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()
{ } /**
* 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()
{ } /**
* 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()
{ }
}

当然现在里面还没有写任何的实现,只是继承了ModelInterface接口。

现在我们先来实现一下findOne方法,在开始实现之前我们要想,我们所有的model都要基于PDO,所以我们应该在model中有一个PDO的实例。所以我们需要在Model类中添加如下变量和方法。

    /**
* @var $pdo PDO instance
*/
public static $pdo; /**
* Get pdo instance
* @return PDO
*/
public static function getDb()
{
if (empty(static::$pdo)) {
$host = 'localhost';
$database = 'sf';
$username = 'jun';
$password = 'jun';
static::$pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
static::$pdo->exec("set names 'utf8'");
} return static::$pdo;
}

用static变量可以保证所有继承该Model的类用的都是同一个PDO实例,getDb方法实现了单例模式(其中的配置暂时hard在这里,在之后的博客里会抽出来),保证了一个请求中,使用getDb只会取到一个PDO实例。

下面我们来实现findOne方法,我们现在定义的findOne有很多局限,例如不支持or,不支持select部分字段,不支持表关联等等。我们之后会慢慢完善这一些内容。其实findOne的实现就是拼接sql语句去执行,直接来看下代码:

    public static function findOne($condition)
{
//  拼接默认的前半段sql语句
$sql = 'select * from ' . static::tableName() . ' where ';
// 取出condition中value作为参数
$params = array_values($condition);
$keys = [];
foreach ($condition as $key => $value) {
array_push($keys, "$key = ?");
}
// 拼接sql完成
$sql .= implode(' and ', $keys);
$stmt = static::getDb()->prepare($sql);
$rs = $stmt->execute($params); if ($rs) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!empty($row)) {
// 创建相应model的实例
$model = new static();
foreach ($row as $rowKey => $rowValue) {
// 给model的属性赋值
$model->$rowKey = $rowValue;
}
return $model;
}
}
// 默认返回null
return null;
}

我们需要来验证一下我们的代码是正确的,先在MySQL中设置一下用户及权限,mock一下数据。

相应的SQL语句如下:

/*创建新用户*/
CREATE USER jun@localhost IDENTIFIED BY 'jun'; /*用户授权 授权jun用户拥有sf数据库的所有权限*/
GRANT ALL PRIVILEGES ON sf.* TO jun@'%' IDENTIFIED BY 'jun'; /*刷新授权*/
FLUSH PRIVILEGES; /*创建数据库*/
CREATE DATABASE IF NOT EXISTS `sf`; /*选择数据库*/
USE `sf`; /*创建表*/
CREATE TABLE IF NOT EXISTS `user` (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(50),
age INT(11),
PRIMARY KEY(id)
); /*插入测试数据*/
INSERT INTO `user` (name, age) VALUES('harry', 20), ('tony', 23), ('tom', 24);

然后再在models文件夹中创建一个User.php,代码如下:

<?php
namespace app\models; use sf\db\Model; /**
* User model
* @property integer $id
* @property string $name
* @property integer $age
*/
class User extends Model
{
public static function tableName()
{
return 'user';
}
}

最后只剩在Controller中使用findOne验证一下了。修改SiteController.php的actionTest方法如下:

    public function actionTest()
{
$user = User::findOne(['age' => 20, 'name' => 'harry']);
$data = [
'first' => 'awesome-php-zh_CN',
'second' => 'simple-framework',
'user' => $user
];
echo $this->toJson($data);
}

打出的如下结果:

{"first":"awesome-php-zh_CN","second":"simple-framework","user":{"id":"1","name":"harry","age":"20"}}

如果将findOne中的条件改成['age' => 20, 'name' => 'tom'],返回值就变为如下结果:

{"first":"awesome-php-zh_CN","second":"simple-framework","user":null}

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

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

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

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

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

    在之前的博客中,我们实现并完善了Model类的findOne方法,下面我们来实现其中的其他方法. 先来看findAll方法,这个方法和findOne很相似. public static functio ...

  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. 【OAuth2.0】Spring Security OAuth2.0篇之初识

    不吐不快 因为项目需求开始接触OAuth2.0授权协议.断断续续接触了有两周左右的时间.不得不吐槽的,依然是自己的学习习惯问题,总是着急想了解一切,习惯性地钻牛角尖去理解小的细节,而不是从宏观上去掌握 ...

  2. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

  3. 浅谈数据库优化方案--表和SQL

    1.数据类型的选择 1.字段最好设置为非空.若字段为char(8),即便是NULL也会现有8个字符的空间. 2.尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能, ...

  4. opengles 矩阵计算

    总的变换矩阵: matrix = projection * view * model 模型矩阵: modelMatrix=translateMatrix * scaleMatrix * rotateM ...

  5. 输入框三种输入方式(selenium webdriver 干货)

    在机票预定的页面,输入出发城市和到达城市输入框的时候, 发现直接使用sendkeys不好使, 大部分情况出现输入某城市后没有输入进去, 经过几天的研究,发现可以采取三种方式: 1. 先点击输入框,待弹 ...

  6. activity与fragment之间传递数据

    总结:无论是activity给fragment传递数据,还是fragment给activity传递数据,都把activity和fragment都当做一个普通的对象,调用它的方法,传递参数. 1.Fra ...

  7. Unity学习疑问记录之协程

    http://blog.csdn.net/huang9012/article/details/38492937 总结:1.协程相当于多线程但不是,(尽管它们看上去是这样的),它们运行在同一线程中,跟普 ...

  8. Hadoop 2.6.0 Namenode HA,ResourceManager HA

    先启动所有的zookeeper zkServer.sh start 在所有节点上启动JournalNode: sbin/hadoop-daemon.sh start journalnode 格式化第一 ...

  9. Principles of measurement of sound intensity

    Introduction In accordance with the definition of instantaneous sound intensity as the product of th ...

  10. Tempdb initial size和dbcc shrinkfile

    在使用sql server时您可能遇到过下面的情况,tempdb的数据文件初始大小为3mb, 随着对tempdb的使用,tempdb文件逐渐变得很大(例如30GB),导致了磁盘空间不足. 此时您需要立 ...