数据源架构模式之行入口模式

在上一篇文章中我们提到了表入口模式,今天我们来讲行入口模式。

在《企业应用架构模式》中对行入口模式的定义是:充当数据源中单条记录入口的对象。每行一个实例。

表入口模式一张表作为一个对象不同,行入口模式使用表中的一行数据作为一个对象。

行数据入口和活动记录模式的主要区别在于行数据入口仅包含数据库访问逻辑而没有领域逻辑,而活动记录则两者皆有。

具体实现如下代码所示:

入口类gameGateway.php

<?php 
require_once 'database.php';
class GameGateway extends Database{

	public $instance;

    private $_id;

    private $_name;

	public $table = 'game';

	public function __construct($id, $name){
		$this->instance = self::getInstance();
        $this->_id = $id;
        $this->_name = $name;
	}

    public function setId($id){
        $this->_id = $id;
    }

    public function getId(){
        return $this->_id;
    }

    public function setName($name){
        $this->_name = $name;
    }

    public function getName(){
        return $this->_name;
    }

    public function insert(){
        $data = array(
            'name' => $this->_name,
            'id' => $this->_id
        );

        $sql = "INSERT INTO $this->table ";
        $sql .= "(" . implode(",", array_keys($data)) . ")";
        $sql .= " VALUES('" . implode("','", array_values($data)) . "')";
        echo $sql;
        return $this->instance->query($sql);
    }

    public function update() {
        $data = array(
            'id' => $this->_id,
            'name' => $this->_name
        );

        $sql = "UPDATE $this->table SET ";
        foreach ($data as $field => $value) {
            $sql .= $field . " = '" . $value . "',";
        }
        $sql = substr($sql, 0, -1);
        $sql .= " WHERE id = " . $this->_id;
        return $this->instance->query($sql);
    }

    public static function load($rs) {
        return new self(!empty($rs['id']) ? $rs['id'] : NULL, $rs['name']);
    }


}
查找类gameFinder.php

<?php
require_once 'database.php';
require_once 'gameGateway.php';
class GameFinder extends Database{

    public $instance;

    public $table = 'game';

    public function __construct(){
        $this->instance = self::getInstance();
    }

    public function find($id) {
        $sql = "SELECT * FROM $this->table WHERE id = " . $id;
        $rs = $this->instance->query($sql)->fetch_assoc();

        return GameGateway::load($rs);//返回行对象
    }

    public function findAll() {
        $sql = "SELECT * FROM $this->table";
        $rs = $this->instance->query($sql);

        $result = array();
        if (is_array($rs)) {
            foreach ($rs as $row) {
                $result[] = GameGateway::load($row);
            }
        }
        return $result;
    }
}
测试代码index.php:

<?php
require_once 'gameGateway.php';
require_once 'gameFinder.php';

//新增
//$data = array('name' => 'new land');
//$game = GameGateway::load($data);
//var_dump($game->insert());

//查询与更新
//$finder = new GameFinder();
//$game = $finder->find(3);
//$game->setName('stormy night');
//var_dump($game->update());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值