在LoginForm中定义了如下方法
class LoginForm extends Model
{
const SCENARIO_LOGIN = 'login';
public $username;
public $password;
public $rememberMe = true;
private $_user;
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_LOGIN] = [
['username', 'password'], 'required'
];
$scenarios[self::SCENARIO_DEFAULT] = [
[['username', 'password'], 'required'],
['rememberMe', 'boolean'],
['password', 'validatePassword'],
];
return $scenarios;
}
获取实例并应用SCENARIO_LOGIN场景,在load时候,进入方法Model.safeAttributes(),进入Model.setAttributes(),进入Model.safeAttributes()方法,该方法中便利了场景,
/**
* Returns the attribute names that are safe to be massively assigned in the current scenario.
* @return string[] safe attribute names
*/
public function safeAttributes()
{
$scenario = $this->getScenario();
$scenarios = $this->scenarios();
if (!isset($scenarios[$scenario])) {
return [];
}
$attributes = [];
foreach ($scenarios[$scenario] as $attribute) {
// 问题在if判断里面
if ($attribute[0] !== '!' && !in_array('!' . $attribute, $scenarios[$scenario])) {
$attributes[] = $attribute;
}
}
return $attributes;
}
上面43行,这行中 '!' . $attribute,由于$scenarios[$scenario]是数组,[['username', 'password'], 'required'],故而报Array convert to string 的错。