[TOC]
# 簡介
在不同的場景下,模型可能會使用不同的業務規則和邏輯, 例如`email`屬性在注冊時強制要求有,但在登陸時不需要;也就是說`User`模型可能會在收集用戶登錄輸入, 也可能會在用戶注冊時使用驗證。
場景特性主要在**驗證**、**屬性塊賦值**或者**基于不同的場景定義不同的 屬性標簽**。
# 使用場景進行驗證
## 定義模型驗證規則
文件在`app\models\Users.php`內容如下:
~~~
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Users extends ActiveRecord
{
const SCENARIO_LOGIN = 'login';
const SCENARIO_REGISTER = 'register';
/**
* @return string
*/
public static function tableName()
{
return 'users';
}
public function rules()
{
return [
// 在"register" 場景下 username, email 和 password 必須有值
[['username', 'email', 'password'], 'required', 'on' => self::SCENARIO_REGISTER],
// 在 "login" 場景下 username 和 password 必須有值
[['username', 'password'], 'required', 'on' => self::SCENARIO_LOGIN],
];
}
}
~~~
### 在控制器中使用
文件在`app\controllers\UserController.php`內容如下:
~~~
<?php
namespace app\controllers;
use app\models\Users;
use yii\web\Controller;
class UserController extends Controller
{
public function actionLogin()
{
$model = new Users;
$model->scenario = Users::SCENARIO_LOGIN;
// 或者通過構造函數配置 $model = new Users(['scenario'=>'login']);
if (\Yii::$app->request->isPost) {
}
return $this->render('login', ['model' => $model]);
}
public function actionRegister()
{
$model = new Users(['scenario'=>'register']);
if (\Yii::$app->request->isPost) {
}
return $this->render('login', ['model' => $model]);
}
}
~~~
# 使用場景進行屬性塊賦值
使用場景進行屬性塊賦值只是在賦值給模塊的`attributes`屬性賦值的時候會根據定義的規則進行賦值。
## 定義模型場景規則
文件在`app\models\Users.php`內容如下
~~~
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Users extends ActiveRecord
{
const SCENARIO_LOGIN = 'login';
const SCENARIO_REGISTER = 'register';
/**
* @return string
*/
public static function tableName()
{
return 'users';
}
/**
* @return array
*/
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_LOGIN] = ['username', 'password'];
$scenarios[self::SCENARIO_REGISTER] = ['username', 'email', 'password'];
return $scenarios;
}
}
~~~
### 控制器代碼
~~~
<?php
namespace app\controllers;
use app\models\Users;
use yii\web\Controller;
class UserController extends Controller
{
public function actionLogin()
{
$model = new Users;
$model->scenario = Users::SCENARIO_LOGIN;
// 或者通過構造函數配置 $model = new Users(['scenario'=>'login']);
if (\Yii::$app->request->isPost) {
$model->attributes = \Yii::$app->request->post('Users');
print_r($model); // 查看model的屬性只有"username"和"password"被賦值
}
return $this->render('login', ['model' => $model]);
}
public function actionRegister()
{
$model = new Users(['scenario'=>'register']);
if (\Yii::$app->request->isPost) {
$model->attributes = \Yii::$app->request->post('Users');
print_r($model);// 查看model的屬性只有"username","email"和"password"被賦值
}
return $this->render('login', ['model' => $model]);
}
}
~~~
# 使用場景定義不同的屬性標簽
屬性標簽是 視圖一部分,但是在模型中申明標簽通常非常方便, 并可形成非常簡潔可重用代碼。
## 定義模型規則
文件在`app\models\Users.php`內容如下(主要查看`attributeLabels()`方法)
~~~
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Users extends ActiveRecord
{
const SCENARIO_LOGIN = 'login';
const SCENARIO_REGISTER = 'register';
/**
* @return string
*/
public static function tableName()
{
return 'users';
}
/**
* @return array
*/
public function attributeLabels()
{
if ($this->scenario == self::SCENARIO_LOGIN) {
$typeString = '登錄';
$userName = $typeString . '用戶名';
$email = $typeString . '郵箱';
$password = $typeString . '密碼';
} else {
$typeString = '注冊';
$userName = $typeString . '名';
$email = $typeString . '郵箱';
$password = $typeString . '密碼';
}
return [
'username' => $userName,
'email' => $email,
'password' => $password,
];
}
}
~~~
## 控制器使用`render()`方法渲染模板文件
~~~
<?php
namespace app\controllers;
use yii\web\Controller;
class UserController extends Controller
{
public function actionLogin()
{
return $this->render('login', ['model' => $model]);
}
public function actionRegister()
{
return $this->render('login', ['model' => $model]);
}
}
~~~
- 目錄
- 配置
- 簡介
- 別名
- gii
- 配置項
- 模型
- 簡介
- 增刪改查
- AR和model
- 模型事件
- 場景
- query查詢
- 增刪改
- AR查詢器
- 模型關系定義
- AR模型連表查詢
- fields
- where拼接
- 模塊
- 創建模塊
- 控制器
- 表單
- 跳轉
- 響應
- 驗證器
- Action
- 組件
- url
- 分頁
- 驗證碼
- 緩存
- 文件上傳
- 預啟動組件
- 事件
- 自定義組件
- redis
- 日志
- 行為
- cookie和session
- 基礎知識
- 創建一個類
- 配置一個類
- object基類
- component組件類特性
- phpstorm無法更改php等級
- url地址美化
- 過濾器
- 請求處理
- 請求組件
- 響應組件
- header
- 用戶登錄
- 實現IdentityInterface接口
- 登錄
- 自動檢測登錄
- 獲取用戶信息
- 訪問行為追蹤
- phpstorm+postman斷點調試