存储的数据无法在Yii中访问 [英] Stored data can't access in Yii

查看:66
本文介绍了存储的数据无法在Yii中访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Yii中创建了表格.为此,我创建了多个复选框.
例如:Fav Sports= Football,Cricket,Baseball
当我选中所有复选框时,它将把记录存储到数据库中.为了存储数据,我使用了"json_encode($_POST['UserInfo']['sports']);".然后它将以["Football"]["Cricket"]["Baseball"]格式存储数据.

I have created form in Yii. In that I've created multiple check boxes.
For Example: Fav Sports= Football,Cricket,Baseball
When I select all check boxes it will store records to database. For storing data I used "json_encode($_POST['UserInfo']['sports']);". Then it will store data in this format ["Football"]["Cricket"]["Baseball"].

我的问题是,当我更新表单页面时,所有文本字段或控件都具有值,但fav sport的复选框仍未选中,因为我无法访问["Football"]["Cricket"]["Baseball"]的数据.如何从Yii中的数据库访问这些记录.

My question is when I go to update form page, all textfields or controls have have values but check boxes of fav sports remains unchecked because I can't able to access data that is ["Football"]["Cricket"]["Baseball"]. How to access those records from database in Yii.

推荐答案

您的模型覆盖如下功能:-

Your Model Override following Function as: -

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>

这篇关于存储的数据无法在Yii中访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆