Yii中Model(模型)的創(chuàng)建及使用方法
本文實例分析了Yii中Model(模型)的創(chuàng)建及使用方法。分享給大家供大家參考,具體如下:
YII 實現(xiàn)了兩種模型,表單模型(CFormModel類)和Active Record模型(CAtiveRecord類),它們都繼承自CModel類。 CFormModel代表的數(shù)據(jù)模型是從HTML表單收集的輸入,封裝了所有邏輯(如表單的驗證和其它業(yè)務(wù)邏輯,應(yīng)用到表單的域上)。它能將數(shù)據(jù)存儲在內(nèi) 存中,或者在一個Active Record的幫助下,存入數(shù)據(jù)庫里。
數(shù)據(jù)庫連接操作
在config/main.php中
'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=oss', 'emulatePrepare' => true, 'username' => 'root', 'password' => 'hahaha', 'charset' => 'utf8', //表前綴 'tablePrefix'=>"oss_" ),
打開注釋,php要支持pdo
查看操作日志
//顯示日志信息,包括sql的查詢信息 array( 'class'=>'CWebLogRoute', ),
將注釋打開
一. 基于CActiveRecord的Model
Active Record(AR) 是一種設(shè)計模式,用面向?qū)ο蟮姆绞匠橄蟮脑L問數(shù)據(jù),Yii中,每一個AR對象的實例都可以是CActiveRecord類或者它的子類。它包裝了數(shù)據(jù)庫表 或視圖中的一行記錄,并封裝了所有的邏輯和風(fēng)聞數(shù)據(jù)庫的細(xì)節(jié),有大部分的業(yè)務(wù)邏輯,必須使用這種模型。數(shù)據(jù)庫表中一行每列字段的值對應(yīng)AR對象的一個屬 性。它將表映射到類,行映射到對象,列則映射到對象的數(shù)據(jù)。也就是說每一個Active Record類的實例代表了數(shù)據(jù)庫中表的一行。但一個 Active Record類不單單是數(shù)據(jù)庫表中的字段跟類中屬性的映射關(guān)系。它還需要在這些數(shù)據(jù)上處理一些業(yè)務(wù)邏輯,定義了所有對數(shù)據(jù)庫的讀寫操作。
1) 聲明一個基于CActiveRecord 類的Model
class Post extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{post}}';
}
public function primaryKey()
{
return 'id';
// return array('pk1', 'pk2');
}
}
2) 使用父類的方法完成數(shù)據(jù)庫操作
(1) Insert:
$post=new Post; $post->title='sample post'; $post->content='content for the sample post'; $post->create_time=time(); $post->save();
(2) Select: 常用幾種方法
// find the first row satisfying the specified condition
$post=Post::model()->find($condition,$params);
// find the row with the specified primary key
$post=Post::model()->findByPk($postID,$condition,$params);
// find the row with the specified attribute values
$post=Post::model()->findByAttributes($attributes,$condition,$params);
// find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);
$criteria=new CDbCriteria;
$criteria->select='title'; // only select the 'title' column
$criteria->condition='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->find($criteria);
$post=Post::model()->find(array(
'select'=>'title',
'condition'=>'postID=:postID',
'params'=>array(':postID'=>10),
));
// find all rows satisfying the specified condition
$posts=Post::model()->findAll($condition,$params);
// find all rows with the specified primary keys
$posts=Post::model()->findAllByPk($postIDs,$condition,$params);
// find all rows with the specified attribute values
$posts=Post::model()->findAllByAttributes($attributes,$condition,$params);
// find all rows using the specified SQL statement
$posts=Post::model()->findAllBySql($sql,$params);
// get the number of rows satisfying the specified condition
$n=Post::model()->count($condition,$params);
// get the number of rows using the specified SQL statement
$n=Post::model()->countBySql($sql,$params);
// check if there is at least a row satisfying the specified condition
$exists=Post::model()->exists($condition,$params);
(3) Update
// update the rows matching the specified condition Post::model()->updateAll($attributes,$condition,$params); // update the rows matching the specified condition and primary key(s) Post::model()->updateByPk($pk,$attributes,$condition,$params); // update counter columns in the rows satisfying the specified conditions Post::model()->updateCounters($counters,$condition,$params);
(4) Delete
$post=Post::model()->findByPk(10); // assuming there is a post whose ID is 10 $post->delete(); // delete the rows matching the specified condition Post::model()->deleteAll($condition,$params); // delete the rows matching the specified condition and primary key(s) Post::model()->deleteByPk($pk,$condition,$params);
(5) 使用事務(wù)
$model=Post::model();
$transaction=$model->dbConnection->beginTransaction();
try
{
// find and save are two steps which may be intervened by another request
// we therefore use a transaction to ensure consistency and integrity
$post=$model->findByPk(10);
$post->title='new post title';
$post->save();
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollBack();
}
二. 基于CFormModel 的Model
編寫表單需要的HTML之前,我們需要決定我們希望用戶輸入哪些數(shù)據(jù),以及應(yīng)該符合什么規(guī)則。一個模型類可以用來記錄這些信息,模型是保持用戶輸入并進(jìn)行驗證的核心
根據(jù)我們?nèi)绾问褂糜脩舻妮斎?,我們可以?chuàng)建兩種類型的模型。如果用戶輸入的數(shù)據(jù)被收集,使用,然后丟棄,我們將創(chuàng)建一個表單模型(form model); 如果用戶輸入的數(shù)據(jù)被保存到數(shù)據(jù)庫中,我們則會使用 active record 。這兩種模型都繼承了他們相同的基類CModel中定義的表單的通用接 口。
1) 模型類的定義
下面的例子中,我們創(chuàng)建了一個LoginForm模型,用來收集用戶在登陸頁面的輸入。由于登陸信息僅僅用于用戶驗證,并不需要保存,因此我們用form model創(chuàng)建
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe=false;
}
LoginForm一共聲明了三個屬性(attributes),$username、$password、$rememberMe
用來記錄用戶輸入的用戶名、密碼、以及是否記住登陸的選項。因為$rememberMe有了默認(rèn)值false,所以顯示表單時對應(yīng)的選框是沒有勾選的。
提示:我們使用名"attributes",而不是"properties",來把他們和正常的屬性(properties)進(jìn)行區(qū)分。
2) 聲明驗證規(guī)則
一旦把用戶提交的數(shù)據(jù)填充到模型,在使用之前,我們要檢查他們是否合法。這是通過對輸入進(jìn)行一組規(guī)則驗證實現(xiàn)的。我們在rulers()方法中通過配置一個數(shù)組來定義驗證規(guī)則
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe=false;
private $_identity;
public function rules()
{
return array(
array('username, password','required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
);
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors()) // we only want to authenticate when no input errors
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect password.');
}
}
}
上面的代碼指明了用戶名和密碼是必須得,密碼需要被驗證,rememberMe必須是布爾型
rules()中返回的每條規(guī)則,必須按照如下格式
array('AttributeList', 'Validator', 'on'=>'ScenarioList', ...附加選項(additional options))
AttributeList 是一個被逗號分隔的需要驗證的屬性名列表。Validator 指出了需要做怎樣的驗證??蛇x的on 參數(shù)指出了該規(guī)則應(yīng)用的場景列表,(additional options)是對應(yīng)的name-value,用于初始對應(yīng)驗證器的相關(guān)屬性
在一個規(guī)則中指定Validator有三種方法,首先Validator可以使該類的一個方法,比如上面例子中的authenticate。該Validator方法必須按照如下的格式聲明
其次 Validator 可以使驗證器的類名,當(dāng)規(guī)則適用時,一個驗證器類的實例會被創(chuàng)建并進(jìn)行實際的驗證。規(guī)則里的附加屬性,用于初始實例的相關(guān)屬性。驗證器類必須繼承于CValidator
提示:當(dāng)對active record模型指定規(guī)則的時候,我們可以使用特殊的參數(shù)‘on',
該參數(shù)可以使'insert' 或者 'update',可以讓規(guī)則分別在插入或者更新的時候適用。如果沒有生命,該規(guī)則會在任何調(diào)用save()的時候適用。
第三、Validator 可以使驗證器類預(yù)先定義的別名。在上面的例子中,“required”便是CRequiredValidator的別名,用來驗證屬性不能為空。下面是預(yù)定義的驗證器類別名的列表
? boolean:CBooleanValidator的別名,驗證屬性的值是否是CBooleanValidator::trueValue 或者 CBooleanValidator::falseValue
? captcha:CCaptchaValidator的別名,驗證屬性的值是否和CAPTCHA中顯示的驗證碼的值相等
? compare:CCompareValidator的別名,驗證屬性的值是否等于另一個屬性或者一個常量
? email:CEmailValidator的別名,驗證屬性的值是否是個合法的email地址
? default:CDefaultValueValidator的別名,為屬性指派一個默認(rèn)值
? exist:CExistValidator的別名,驗證屬性的值是否能在表的列里面找到
? file: CFileValidator 的別名, 驗證屬性是否包含上傳文件的名字
? filter:CFilterValidator的別名,使用一個過濾器轉(zhuǎn)換屬性的形式
? in: CRangeValidator 的別名, 驗證屬性值是否在一個預(yù)訂的值列表里面
? length: CStringValidator 的別名, 確保了屬性值的長度在指定的范圍內(nèi).
? match: CRegularExpressionValidator 的別名, 驗證屬性是否匹配一個正則表達(dá)式.
? numerical: CNumberValidator 的別名, 驗證屬性是否是一個有效的數(shù)字.
? required: CRequiredValidator 的別名, 驗證屬性的值是否為空.
? type: CTypeValidator 的別名, 驗證屬性是否是指定的數(shù)據(jù)類型.
? unique: CUniqueValidator 的別名, 驗證屬性在數(shù)據(jù)表字段中是否是唯一的.
? url: CUrlValidator 的別名, 驗證屬性是否是一個有效的URL路徑.
下面我們給出一些使用預(yù)定義驗證器的例子。
// username is required
array('username', 'required'),
// username must be between 3 and 12 characters
array('username', 'length', 'min'=>3, 'max'=>12),
// when in register scenario, password must match password2
array('password', 'compare', 'compareAttribute'=>'password2',
'on'=>'register'),
// when in login scenario, password must be authenticated
array('password', 'authenticate', 'on'=>'login'),
3) 安全屬性的設(shè)置
當(dāng)一個模型創(chuàng)建之后,我們往往需要根據(jù)用戶的輸入,為它填充屬性。這可以方便的通過下面批量賦值的方式來實現(xiàn)
$model=new LoginForm; if(isset($_POST['LoginForm'])) $model->attributes=$_POST['LoginForm'];
最后那條語句便是批量賦值,把$_POST['LoginForm']中每個屬性都賦值到對應(yīng)的模型屬性中,它等價于下面的語句
foreach($_POST['LoginForm'] as $name=>$value)
{
if($name is a safe attribute)
$model->$name=$value;
}
聲明屬性是否是安全屬性是個至關(guān)重要的工作。例如,如果我把把數(shù)據(jù)表的主鍵暴露為安全屬性,那么便可以通過修改主鍵的值,來管理本沒有權(quán)限管理的數(shù)據(jù),進(jìn)行攻擊。
4) 1.1版中的安全屬性
在1.1版中,如果屬性在適用的規(guī)則中指定了驗證器,則認(rèn)為是安全的。例如
array('username, password', 'required', 'on'=>'login, register'),
array('email', 'required', 'on'=>'register'),
上面的代碼中用戶名和密碼屬性在login的場景下不允許為空。用戶名、密碼郵箱在register的場景下不允許為空。因此如果在login的場景下 進(jìn) 行批量賦值,僅僅用戶名和密碼會被賦值,因為login場景下驗證規(guī)則里僅出現(xiàn)了這兩個屬性,但是如果是在register場景下,那么這三個屬性都 會被 賦值。
// in login scenario
$model=new User('login');
if(isset($_POST['User']))
$model->attributes=$_POST['User'];
// in register scenario
$model=new User('register');
if(isset($_POST['User']))
$model->attributes=$_POST['User'];
那么為什么我們使用如此的策略來決定一個屬性是否是安全屬性呢?因為一個屬性,已經(jīng)有了一個或者多個對個進(jìn)行校驗的規(guī)則,那么我還需要擔(dān)心嗎?
需要記住的是,驗證器是用來檢測用戶輸入的數(shù)據(jù),而不是我們用代碼產(chǎn)生的數(shù)據(jù)(例如 時間戳,自增的主鍵等)。因此不要給那些不需要用戶輸入的屬性添加驗證器。
有時候我們想聲明一些屬性為安全屬性,但是又不必給指定一個驗證規(guī)則。例如文章的正文屬性,我們可以允許用戶的任何輸入。為了實現(xiàn)這個目標(biāo),我們可以用safe規(guī)則。
對應(yīng)的也有一個unsafe規(guī)則,來指定哪些屬性是不安全的
unsafe并不常用,對你以前定義的安全屬性來說,這是個例外
5) 獲取驗證錯誤
當(dāng)驗證結(jié)束后,任何可能的錯誤都存儲在模型的實例中。我們可以通過調(diào)用CModel::getErrors() 和 CModel::getError()重新獲取到。這兩個方法的區(qū)別在于,第一個可以返回指定模型屬性的所有錯誤,而第二個方法只返回了第一條錯誤。
6) 屬性標(biāo)簽
設(shè)計表單的時候,我們需要為用戶的輸入框顯示一個標(biāo)簽,來提示用戶輸入。盡管我們可以再form中寫死,但是如果我們在相應(yīng)的模型中指定的話會更加方便和靈活
默認(rèn)情況下,CModel 會簡單的返回屬性的名字作為標(biāo)簽。這可以通過重寫attributeLabels() 方法來自定義。在接下來章節(jié)中我們將看到,在模型中指定標(biāo)簽可以讓我們更快更強大的創(chuàng)建一個form表單
希望本文所述對大家基于yii框架的php程序設(shè)計有所幫助。
- YII2框架中自定義用戶認(rèn)證模型,完成登陸和注冊操作示例
- Yii2.0框架模型添加/修改/刪除數(shù)據(jù)操作示例
- Yii2.0框架模型多表關(guān)聯(lián)查詢示例
- yii框架表單模型使用及以數(shù)組形式提交表單數(shù)據(jù)示例
- PHP YII框架開發(fā)小技巧之模型(models)中rules自定義驗證規(guī)則
- PHP的Yii框架中Model模型的學(xué)習(xí)教程
- Yii框架數(shù)據(jù)模型的驗證規(guī)則rules()被執(zhí)行的方法
- YII動態(tài)模型(動態(tài)表名)支持分析
- Yii框架表單模型和驗證用法
- Yii模型操作之criteria查找數(shù)據(jù)庫的方法
- Yii數(shù)據(jù)模型中rules類驗證器用法分析
- yii2.0框架多模型操作示例【添加/修改/刪除】
相關(guān)文章
PHP運用foreach神奇的轉(zhuǎn)換數(shù)組(實例講解)
下面小編就為大家分享一篇PHP運用foreach神奇的轉(zhuǎn)換數(shù)組(實例講解),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
yii實現(xiàn)CheckBox復(fù)選框在同一行顯示的方法
這篇文章主要介紹了yii實現(xiàn)CheckBox復(fù)選框在同一行顯示的方法,對比了網(wǎng)上搜集的方法,給出了改進(jìn)的意見,非常具有實用價值,需要的朋友可以參考下2014-12-12
Laravel ORM 數(shù)據(jù)model操作教程
今天小編就為大家分享一篇Laravel ORM 數(shù)據(jù)model操作教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php實現(xiàn)爬取和分析知乎用戶數(shù)據(jù)
本文給大家介紹的是利用php的curl編寫的爬取知乎用戶數(shù)據(jù)的爬蟲,并分析用戶的各種屬性,有需要的小伙伴可以參考下2016-01-01
Laravel框架實現(xiàn)model層的增刪改查(CURD)操作示例
這篇文章主要介紹了Laravel框架實現(xiàn)model層的增刪改查(CURD)操作,結(jié)合實例形式分析了Laravel框架模型model層進(jìn)行數(shù)據(jù)庫的增刪改查操作具體實現(xiàn)技巧,需要的朋友可以參考下2018-05-05

