適用於yii1.1的thinkphp化的db操做類

前段時間的一個項目是要對一個用yii1.1寫的商城作修改,不怎麼好寫,不過比面向過程的ecshop好得多,以前剛開始改ecshop還真是頭大php

yii1.1其中數據庫操做花了我很多時間去找各類例子進行測試,出於往後可能還要改的緣由,我將數據庫的一些操做改爲了thinkphp的形式,代碼以下thinkphp

<?php
/**gcud數據庫操做
 * @version 20191231
 */
 
class gcudDb
{
    private $TableName;
    private $Conditions;
    private $ConditionsParameters;
    private $FieldsString;

    private function getDbCommand()
    {
        return \Yii::app()->db->createCommand();
    }

    private function BuildCondition()
    {
        $this->ConditionsParameters = [];
        $this->ConditionsParameters['fieldString'] = '';
        $this->ConditionsParameters['parameters'] = [];
        if ($this->Conditions)
            foreach ($this->Conditions as $Key => $Value) {
                $this->ConditionsParameters['fieldString'] .= " and $Key=:$Key";
                $this->ConditionsParameters['parameters'][":$Key"] = $Value;
            }
        $this->ConditionsParameters['fieldString'] = ltrim($this->ConditionsParameters['fieldString'], ' and ');
    }

    public static function name($TableName)
    {
        $Table = new static();
        $Table->TableName = "{{_$TableName}}";
        return $Table;
    }

    public function field($FieldString)
    {
        $this->FieldsString = $FieldString;
        return $this;
    }

    public function where($field, $value)
    {
        $this->Conditions[$field] = $value;
        return $this;
    }

    public function insert($Data)
    {
        self::getDbCommand()->insert($this->TableName, $Data);
    }

    public function delete()
    {
        $this->BuildCondition();
        self::getDbCommand()->delete($this->TableName, $this->ConditionsParameters['fieldString'],
            $this->ConditionsParameters['parameters']);
    }

    public function find()
    {
        $this->BuildCondition();
        return self::getDbCommand()->select($this->FieldsString)
            ->from($this->TableName)->where($this->ConditionsParameters['fieldString'],
                $this->ConditionsParameters['parameters'])->queryRow();
    }

    public function column($Field)
    {
        $this->BuildCondition();
        $Data = self::getDbCommand()->select($Field)
            ->from($this->TableName)->where($this->ConditionsParameters['fieldString'],
                $this->ConditionsParameters['parameters'])->queryAll();
        $ReturnData = null;
        foreach ($Data as $Item) {
            $ReturnData[] = $Item[$Field];
        }
        return $ReturnData;
    }

    public function value($Field)
    {
        $this->BuildCondition();
        $Data = self::getDbCommand()->select($Field)->from($this->TableName)
            ->where($this->ConditionsParameters['fieldString'],
                $this->ConditionsParameters['parameters'])->queryRow();
        return $Data[$Field];
    }

    public function count()
    {
        $this->BuildCondition();
        $Data = self::getDbCommand()->select('count(1) gcudcount')
            ->from($this->TableName)->where($this->ConditionsParameters['fieldString'],
                $this->ConditionsParameters['parameters'])->queryRow();
        return $Data['gcudcount'];
    }

    public function update($Data)
    {
        $this->BuildCondition();
        self::getDbCommand()->update($this->TableName, $Data,
            $this->ConditionsParameters['fieldString'],
            $this->ConditionsParameters['parameters']);
    }
}

db操做風格是5.1的,where操做僅支持直接的deng'yu數據庫

相關文章
相關標籤/搜索