初探PHP&Yii(一)

最近在學PHP的Yii框架,記錄一下學習收穫方便以後查看。

一、Yii框架採用的是MVC架構模式分:Model\View\Controler 三大塊。

        什麼事MVC模式?簡單實用地說:如果視圖這裏看的有點莫名其妙的時候先去看下控制器。視圖的展現與控制器息息相關。視圖的邏輯不清的地方在控制器會有答案。

二、訪問數據庫有兩種方式,

       1、對象數據庫方式,主要是基於Yii框架的ActiveRecord類來實現,

      eg:

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "EquipmentOfProperty".
 *
 * @property string $EquipmentOfPropertyId
 * @property string $PropertyId
 * @property string $EquipmentId
 * @property int $SortOrder
 */
class EquipmentOfProperty extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'EquipmentOfProperty';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['EquipmentOfPropertyId', 'PropertyId', 'EquipmentId'], 'required'],
            [['EquipmentOfPropertyId', 'PropertyId', 'EquipmentId', 'SortOrder'], 'integer'],
            [['EquipmentOfPropertyId'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'EquipmentOfPropertyId' => Yii::t('app', 'Equipment Of Property ID'),
            'PropertyId' => Yii::t('app', 'Property ID'),
            'EquipmentId' => Yii::t('app', 'Equipment ID'),
            'SortOrder' => Yii::t('app', 'Sort Order'),
        ];
    }
}

        優點:1)這種方式訪問數據庫簡單,對於一些對數據庫不熟悉的人容易上手。

                   2)較大吻合面向對象編程思想,能容易理解

        缺點:創建對象比較多,創建過程中耗費時間與系統資源,不是很合適訪問量非常大響應度非常高的服務。

        2、直接訪問數據庫模式,主要是基於Yii框架的SqlDataProvider類來實現,

<?php

namespace app\modules\process\models\search;

use app\modules\process\models\ProductionRule;
use Yii;

class ProductionRuleDataProvider extends ProductionRule
{
    public function search($params)
    {
        $sql = 'SELECT RuleId,Description,Version FROM ProductionRule';

        $count = Yii::$app->db->createCommand('SELECT COUNT(RuleId) FROM ProductionRule')->queryScalar();

        $dataProvider = new \yii\data\SqlDataProvider([
            'pagination' => false,
            'sql' => $sql,
            'key' => 'RuleId',
            'totalCount' => $count,
            'sort' => [
                'attributes' => [
                    'PublishedDate' => [
                        'default' => SORT_DESC,
                    ],
                ],
                'defaultOrder' => [
                    'PublishedDate' => SORT_DESC,
                ],
            ],
        ]);

        return $dataProvider;
    }

    private function findProductionRule($id){
        return ProductionRule::findOne(['RuleId'=>$id])!==null;
    }

    public function firstKey($id){
        try {
            if($this->findProductionRule($id)) return $id;

            $command = Yii::$app->db->createCommand('SELECT RuleId FROM ProductionRule');

            $id = $command->queryScalar();
            if(isset($id) && $id !== false) return $id;
            
        } catch (Exception $exc) {
            \app\helpers\Verification::throwException($e->getMessage());
        }

        return 0;
    }
}

        優點:高效

        缺點:必須對SQL語句,對數據庫後臺表結構比較清楚

三、PHP嚴格區分大小寫,數據庫都必須一致

       如果以前使用其他語言的時候不太注意大小寫的童鞋這點會比較不適應,而且容易出現問題。

      譬如:數據庫字段是EquipmentId,代碼中寫的是EquipmentID則會成爲是別不到的字段而報錯。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章