我已经在 Yii 中创建了表单。为此,我创建了多个复选框。
例如:Fav Sports= Football,Cricket,Baseball
当我选中所有复选框时,它会将记录存储到数据库中。为了存储数据,我使用了 "json_encode($_POST['UserInfo']['sports']);"
。然后它将以这种格式存储数据["Football"]["Cricket"]["Baseball"]
。
我的问题是,当我去更新表单页面时,所有文本字段或控件都有值,但 fav sports 的复选框仍未选中,因为我无法访问 ["Football"]["板球”][“棒球”]
。如何从 Yii 中的数据库访问这些记录。
最佳答案
您的模型将以下函数重写为:-
public function beforeSave() {
$this->sports = CJSON::encode($this->sports);
return true;
}
public function afterSave() {
$this->sports = CJSON::decode($this->sports, TRUE);
return true;
}
public function afterFind() {
$this->username = CJSON::decode($this->username, TRUE);
return true;
}
在你看来
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->checkBoxList($model, 'sports', array('Football' => 'Football', 'Cricket' => 'Cricket','Baseball' => 'BaseBall')); ?>
</div>
关于php - 存储的数据无法在 Yii 中访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221063/