设计模式:DAO 数据存取对象

DAO数据存取对象一般都是虚类,并且与单例模式的数据库连接对象配合使用。

<?php

require_once 'Base.MySQLConnection.php';

interface IBaseDAO
{
    public function Charset($charset);
    public function Query($sql);
    public function Fetch($fields, $conditions, $conditionsDelim, $opt);
    public function Update($id, $keyedValueArray);
}


abstract BaseDAO implements IBaseDAO
{
    public function Charset($charset = 'utf8')
    {
        $this->Query("set names {$charset}");
    }
    public function Query($sql = '')
    {
        $result = null;
        if ($sql && is_string($sql) && $this->connection)
        {
            $result = @mysqli_query($this->connection, $sql);
        }
        return $result;
    }

    public function Fetch($fields = '*', $conditions = null, $conditionsDelim = 'and', $opt = MYSQLI_ASSOC)
    {
        records = null;

        $sqlConditions = '';
        if ($conditions)
            $whereAs = '';
            if (is_array($conditions))
            {
                $conditionsArray = array();
                foreach($conditions as $key => $value)
                {
                    $conditionsArray[] = "{$key}={$value}";
                }
                $whereAs = implode(" {$conditionsDelim} ", $conditionsArray);
            }
            else if (is_string($conditions))
            {
                $whereAs = $conditions;
            }

            if ($whereAs)
            {
                $sqlConditions = "where {$whereAs}"
            }
        }

        $sql = "select {$strFields} from {$this->queryTable} {$sqlConditions} limit {$this->limitRecordsQty}";
        $queryResult = $this->Query($sql);
        if ($queryResult)
        {
            $queryRecord = mysqli_fetch_array($queryResult, $opt);
            while($queryRecord)
            {
                records[] = $queryRecords;
                $queryRecord = mysqli_fetch_array($queryResult, $opt);
            }
        }
        return records;
    }

    public function Update($keyId = 0, $keydValueArray = null)
    {
        if ($keyId > 0)
        {
            if (is_array($keyedValueArray))
            {
                $updates = '';
                $items = array();
                foreach($keyedValueArray as $key => $value)
                {
                    $items[] = "{$key}='{$value}'";
                }
                $updates = implode(',', $items);
                $sql = "update {$this->queryTable} set {$updates} where {$this->queryPrimaryKey}='{$keyId}'";
                return $this->Query($sql);
            }
        }
        return null;
    }
}


final class NMSDevice extends BaseDAO
{
    private $connection;

    private $queryTable, $queryPrimaryKey;
    private $limitRecordsQty = 10;

    public function __construct()
    {
        $this->connections = MySQLConnection::GetInstance();
        $this->queryTable = 'arn_device';
        $this->queryPrimaryKey = 'id';
    }

    public function List()
    {
        $fields = array('id', 'name', 'mac', 'ip', 'ts');
        return $this->Fetch($fields);
    }

    public function __destruct()
    {
        // this will call -MySQLConnection::close();
        $this->connection = null;
    }
}


$device = new NMSDevice();
$data = device->List();

?>

results matching ""

    No results matching ""