Php Yii2:如何在checkboxList()中使用带有逗号的属性。值列表
我有一个属性,其中数据库值为,例如:现金、信用卡、贝宝。在表单上,这当然需要作为每个选项的复选框呈现,因此我假设我需要这样做:Php Yii2:如何在checkboxList()中使用带有逗号的属性。值列表,php,yii2,checkboxlist,Php,Yii2,Checkboxlist,我有一个属性,其中数据库值为,例如:现金、信用卡、贝宝。在表单上,这当然需要作为每个选项的复选框呈现,因此我假设我需要这样做: echo$form->field($model,'payment\u options')) ->复选框列表(['cash'=>'cash','creditcard'=>'creditcard','paypal'=>'paypal','bitcoin'=>'bitcoin']); 但默认情况下,没有选中任何复选框。我如何指导Yii使用逗号拆分(分解)值?我想,在插入回数
echo$form->field($model,'payment\u options'))
->复选框列表(['cash'=>'cash','creditcard'=>'creditcard','paypal'=>'paypal','bitcoin'=>'bitcoin']);
但默认情况下,没有选中任何复选框。我如何指导Yii使用逗号拆分(分解)值?我想,在插入回数据库之前,是否再次连接(内爆)
在中,我看到了一些关于数组属性的信息,但是我没有找到关于如何处理这些属性的其他信息…我发现这是最好的、最有组织的方法 创建一个(例如,在
components
文件夹中创建文件ArrayAttributes.php
,并设置namespace-app\components;
):
使用yii\db\ActiveRecord;
使用yii\base\Behavior;
/**
*用于处理数组属性,是数据库中以逗号分隔的值列表
*另外一个特性是处理JSON字符串,例如:{“性别”:“req”,“生日”:“hide”,“addr”:“req”,“zip”:“req”,“city”:“req”,“state”:“opt”}
*/
类ArrayAttributesBehavior扩展了行为{
公共$attributes=[];
公共$分隔符=',';
公共$jsonAttributes=[];
公共活动(){
返回[
ActiveRecord::EVENT\u AFTER\u FIND=>“toArrays”,
ActiveRecord::事件在验证之前=>'toArrays',
ActiveRecord::事件插入之前的事件=>'ToString',
ActiveRecord::事件更新之前的事件=>'ToString',
];
}
公共活动安排($event){
foreach($this->attributes as$attribute){
如果($this->owner->$attribute){
$this->owner->$attribute=explode($this->separator,$this->owner->$attribute);
}否则{
$this->owner->$attribute=[];
}
}
foreach($this->jsonAttributes as$attribute){
如果(是字符串($this->owner->$attribute)){
$this->owner->$attribute=json\u decode($this->owner->$attribute,true);
}
}
}
公共函数到字符串($event){
foreach($this->attributes as$attribute){
if(是数组($this->owner->$attribute)){
$this->owner->$attribute=内爆($this->separator,$this->owner->$attribute);
}
}
foreach($this->jsonAttributes as$attribute){
如果(!是字符串($this->owner->$attribute)){
$this->owner->$attribute=json\u encode($this->owner->$attribute);
}
}
}
}
然后在模型中对其进行配置:
public function behaviors() {
return [
[
'class' => \your\namespace\ArrayAttributesBehavior::className(),
'attributes' => ['payment_options'],
],
];
}
然后请记住,在制作表单、验证等时,这些属性都是数组。我发现这是最好、最有组织的方法 创建一个(例如,在
components
文件夹中创建文件ArrayAttributes.php
,并设置namespace-app\components;
):
使用yii\db\ActiveRecord;
使用yii\base\Behavior;
/**
*用于处理数组属性,是数据库中以逗号分隔的值列表
*另外一个特性是处理JSON字符串,例如:{“性别”:“req”,“生日”:“hide”,“addr”:“req”,“zip”:“req”,“city”:“req”,“state”:“opt”}
*/
类ArrayAttributesBehavior扩展了行为{
公共$attributes=[];
公共$分隔符=',';
公共$jsonAttributes=[];
公共活动(){
返回[
ActiveRecord::EVENT\u AFTER\u FIND=>“toArrays”,
ActiveRecord::事件在验证之前=>'toArrays',
ActiveRecord::事件插入之前的事件=>'ToString',
ActiveRecord::事件更新之前的事件=>'ToString',
];
}
公共活动安排($event){
foreach($this->attributes as$attribute){
如果($this->owner->$attribute){
$this->owner->$attribute=explode($this->separator,$this->owner->$attribute);
}否则{
$this->owner->$attribute=[];
}
}
foreach($this->jsonAttributes as$attribute){
如果(是字符串($this->owner->$attribute)){
$this->owner->$attribute=json\u decode($this->owner->$attribute,true);
}
}
}
公共函数到字符串($event){
foreach($this->attributes as$attribute){
if(是数组($this->owner->$attribute)){
$this->owner->$attribute=内爆($this->separator,$this->owner->$attribute);
}
}
foreach($this->jsonAttributes as$attribute){
如果(!是字符串($this->owner->$attribute)){
$this->owner->$attribute=json\u encode($this->owner->$attribute);
}
}
}
}
然后在模型中对其进行配置:
public function behaviors() {
return [
[
'class' => \your\namespace\ArrayAttributesBehavior::className(),
'attributes' => ['payment_options'],
],
];
}
然后请记住,在制作表单、验证等时,这些属性都是数组