php - 存储的数据无法在 Yii 中访问

标签 php mysql yii

我已经在 Yii 中创建了表单。为此,我创建了多个复选框。
例如:Fav Sports= Football,Cricket,Baseball
当我选中所有复选框时,它会将记录存储到数据库中。为了存储数据,我使用了 "json_encode($_POST['U​​serInfo']['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/

相关文章:

php - 附加 where 子句

mysql - 删除数据库表

php - 只有在 DELETE 期间被删除并且是最后一个受影响的用户时,如何添加新的 MYSQL 行?

php - 在我的本地主机上安装crete5时出现SQL错误

php - magento 付款流程.. 一般如何运作

php - 替换不间断空格 PHP

mysql - 导出 mysql 数据库时 mysql 表中的日期值发生变化

php - yii2 advanced 中的公共(public)资源放在哪里?

php - 您如何动态显示用户配置文件?

php - Yii 1.1.7 - 找不到 gii 页面