Yii2 设置方案不起作用
Yii2 set scenario is not working
我想为我的其余应用程序使用场景的操作之一添加额外的字段。这就是我所做的
控制器动作
$model =(new Job(['scenario' => Job::SCENARIO_MORE]))->findOne(['id'=>$id]);
if ($model){
return $model;
}
型号代码
const SCENARIO_LESS = 'index';
const SCENARIO_MORE = 'view';
public function scenarios()
{
return [
self::SCENARIO_LESS => ['field1', 'field2'],
self::SCENARIO_MORE => ['field1', 'field2', 'field3'],
];
}
但它仍然返回默认字段,没有发生任何变化吗?
我认为场景仅用于在将数据插入 table
之前验证数据
The scenario feature is primarily used by validation and massive attribute assignment. You can, however, use it for other purposes. For example, you may declare attribute labels differently based on the current scenario.
但是如果你想要特定的字段,你应该在模型中使用 fields
方法:
// explicitly list every field, best used when you want to make sure the changes
// in your DB table or model attributes do not cause your field changes (to keep API backward compatibility).
public function fields()
{
return [
// field name is the same as the attribute name
'id',
// field name is "email", the corresponding attribute name is "email_address"
'email' => 'email_address',
// field name is "name", its value is defined by a PHP callback
'name' => function () {
return $this->first_name . ' ' . $this->last_name;
},
];
}
// filter out some fields, best used when you want to inherit the parent implementation
// and blacklist some sensitive fields.
public function fields()
{
$fields = parent::fields();
// remove fields that contain sensitive information
unset($fields['auth_key'], $fields['password_hash'], $fields['password_reset_token']);
return $fields;
}
参见:http://www.yiiframework.com/doc-2.0/guide-structure-models.html#fields
我想为我的其余应用程序使用场景的操作之一添加额外的字段。这就是我所做的
控制器动作
$model =(new Job(['scenario' => Job::SCENARIO_MORE]))->findOne(['id'=>$id]);
if ($model){
return $model;
}
型号代码
const SCENARIO_LESS = 'index';
const SCENARIO_MORE = 'view';
public function scenarios()
{
return [
self::SCENARIO_LESS => ['field1', 'field2'],
self::SCENARIO_MORE => ['field1', 'field2', 'field3'],
];
}
但它仍然返回默认字段,没有发生任何变化吗?
我认为场景仅用于在将数据插入 table
之前验证数据The scenario feature is primarily used by validation and massive attribute assignment. You can, however, use it for other purposes. For example, you may declare attribute labels differently based on the current scenario.
但是如果你想要特定的字段,你应该在模型中使用 fields
方法:
// explicitly list every field, best used when you want to make sure the changes
// in your DB table or model attributes do not cause your field changes (to keep API backward compatibility).
public function fields()
{
return [
// field name is the same as the attribute name
'id',
// field name is "email", the corresponding attribute name is "email_address"
'email' => 'email_address',
// field name is "name", its value is defined by a PHP callback
'name' => function () {
return $this->first_name . ' ' . $this->last_name;
},
];
}
// filter out some fields, best used when you want to inherit the parent implementation
// and blacklist some sensitive fields.
public function fields()
{
$fields = parent::fields();
// remove fields that contain sensitive information
unset($fields['auth_key'], $fields['password_hash'], $fields['password_reset_token']);
return $fields;
}
参见:http://www.yiiframework.com/doc-2.0/guide-structure-models.html#fields