php yii model,Yii模型
屬性
屬性代表業(yè)務(wù)數(shù)據(jù)。它們可以像數(shù)組元素或?qū)ο蟮膶傩阅菢觼碓L問。一個模型的每個屬性都是公開訪問的屬性。要指定模型擁有什么屬性,應(yīng)該重寫yii\base\Model::attributes()?方法。
我們看一下基本應(yīng)用程序模板中的?ContactForm?模型。
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model {
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* @return array the validation rules.
*/
public function rules() {
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
* @return array customized attribute labels
*/
public function attributeLabels() {
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information
collected by this model.
* @param string $email the target email address
* @return boolean whether the model passes validation
*/
public function contact($email) {
if ($this->validate()) {
Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}
?>
第1步?-?在 SiteController中創(chuàng)建一個?actionShowContactModel?函數(shù)并使用下面的代碼。
public function actionShowContactModel() {
$mContactForm = new \app\models\ContactForm();
$mContactForm->name = "contactForm";
$mContactForm->email = "user@gmail.com";
$mContactForm->subject = "標(biāo)題";
$mContactForm->body = "內(nèi)容主體";
var_dump($mContactForm);
}
在上面的代碼中,我們定義?ContactForm?表單模型,設(shè)置屬性,并在屏幕上顯示模型。
如模型是從?yii\base\Model?擴(kuò)展,那么它的所有成員變量應(yīng)該為公共且是非靜態(tài)的屬性。在?ContactForm?模型五個屬性?-?name,?email,?subject,?body,?verifyCode,也可以再添加一些新的。
屬性標(biāo)簽
在應(yīng)用中我們經(jīng)常需要使用屬性相關(guān)聯(lián)來顯示標(biāo)簽。默認(rèn)情況下,屬性標(biāo)簽由 yii\base\Model::generateAttributeLabel()?方法自動生成。要手動聲明屬性標(biāo)簽,可以覆蓋yii\base\Model::attributeLabels()?方法。
注意,標(biāo)簽的名稱與屬性的名稱相同。
步驟2?-?現(xiàn)在,中以下列方式修改在?ContactForm?模型中的?attributeLabels() 函數(shù)。
public function attributeLabels() {
return [
'name' => '名字',
'email' => '郵箱地址',
'subject' => '標(biāo)題',
'body' => '內(nèi)容',
'verifyCode' => '驗證碼',
];
}
模型使用在不同的場景
可以使用模型在不同的場景。?例如,當(dāng)一個訪問用戶要發(fā)送一份聯(lián)系表單,我們需要所有的模型屬性。?當(dāng)用戶已經(jīng)登錄,我們并不需要他的名字,因為我們可以很容易地從數(shù)據(jù)庫把它讀取出來。
要聲明場景,應(yīng)該覆蓋?scenarios()?函數(shù)。它返回一個數(shù)組,其鍵是場景名稱而其值是?Active?屬性。Active屬性是用來來驗證的。它們也可以被大量分配。
第1步?-?以下方式修改?ContactForm?模型。
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model {
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
const SCENARIO_EMAIL_FROM_GUEST = 'EMAIL_FROM_GUEST';
const SCENARIO_EMAIL_FROM_USER = 'EMAIL_FROM_USER';
public function scenarios() {
return [
self::SCENARIO_EMAIL_FROM_GUEST => ['name', 'email', 'subject',
'body', 'verifyCode'],
self::SCENARIO_EMAIL_FROM_USER => ['email' ,'subject', 'body',
'verifyCode'],
];
}
/**
* @return array the validation rules.
*/
public function rules() {
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
* @return array customized attribute labels
*/
public function attributeLabels() {
return [
'name' => '名字',
'email' => '電子郵箱',
'subject' => '標(biāo)題',
'body' => '內(nèi)容',
'verifyCode' => '驗證碼',
];
}
/**
* Sends an email to the specified email address using the information
collected by this model.
* @param string $email the target email address
* @return boolean whether the model passes validation
*/
public function contact($email) {
if ($this -> validate()) {
Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}
?>
我們增加了兩個場景。一個用于訪問游客用戶,另一個用于身份驗證的用戶。當(dāng)用戶通過驗證后,再不需要他填入名字。
第2步-?現(xiàn)在,修改 SiteController 的?actionContact?功能。
public function actionContact() {
$model = new ContactForm();
$model->scenario = ContactForm::SCENARIO_EMAIL_FROM_GUEST;
if ($model->load(Yii::$app->request->post()) && $model->
contact(Yii::$app->params ['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
第4步?-?如果你在模式的?actionContact?動作中更改了場景。如下面給出的代碼,你會發(fā)現(xiàn)?name?屬性不再是必需的。
$model->scenario = ContactForm::SCENARIO_EMAIL_FROM_USER;
大量的分配
大規(guī)模的分配是通過一個單一的代碼行創(chuàng)建多個輸入模型屬性的一種便捷方式。
如下的代碼行?-
$mContactForm = new \app\models\ContactForm;
$mContactForm->attributes = \Yii::$app->request->post('ContactForm');
上面給出的代碼行相當(dāng)于-
$mContactForm = new \app\models\ContactForm;
$postData = \Yii::$app->request->post('ContactForm', []);
$mContactForm->name = isset($postData['name']) ? $postData['name'] : null;
$mContactForm->email = isset($postData['email']) ? $postData['email'] : null;
$mContactForm->subject = isset($postData['subject']) ? $postData['subject'] : null;
$mContactForm->body = isset($postData['body']) ? $postData['body'] : null;
前者干凈多了。注意,大量分配僅適用于安全屬性。它們只是在?scenario()?函數(shù)中列出當(dāng)前場景屬性。
數(shù)據(jù)導(dǎo)出
模型往往需要以不同的格式導(dǎo)出。要轉(zhuǎn)模型轉(zhuǎn)換為數(shù)組,則修改?SiteController?的?actionShowContactModel()?函數(shù)-
public function actionShowContactModel() {
$mContactForm = new \app\models\ContactForm();
$mContactForm->name = "用戶名稱";
$mContactForm->email = "user@gmail.com";
$mContactForm->subject = "標(biāo)題";
$mContactForm->body = "內(nèi)容";
var_dump($mContactForm->attributes);
}
要將模型轉(zhuǎn)換成JSON格式,修改actionShowContactModel()函數(shù)如以下方式-
public function actionShowContactModel() {
$mContactForm = new \app\models\ContactForm();
$mContactForm->name = "username";
$mContactForm->email = "user@gmail.com";
$mContactForm->subject = "subject";
$mContactForm->body = "body-content";
return \yii\helpers\Json::encode($mContactForm);
}
瀏覽器輸出?-
要點
模型通常是一個精心設(shè)計比控制器快得多的應(yīng)用。模型應(yīng)該-
包含業(yè)務(wù)邏輯
包含驗證規(guī)則
包含屬性
不嵌入HTML
不能直接訪問請求
不要有太多的場景
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的php yii model,Yii模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推荐一个配置linux服务的网站
- 下一篇: 每天一道CTF---bugku-----