【问题标题】:How to save content from yii widget to Database?如何将内容从 yii 小部件保存到数据库?
【发布时间】:2014-04-10 19:37:51
【问题描述】:

这是 ListBuilder Widget 代码:

$this->widget('ext.widgets.multiselects.XMultiSelects',array(
'leftTitle'=>'Australia',
'leftName'=>'Person[australia][]',
'leftList'=>Person::model()->findUsersByCountry(14),
'rightTitle'=>'New Zealand',
'rightName'=>'Person[newzealand][]',
'rightList'=>Person::model()->findUsersByCountry(158),
'size'=>20,
'width'=>'200px',
));

列表生成器视图this

我想将我选择的整个列表保存到我的数据库中。 如何做到这一点?

【问题讨论】:

    标签: yii yii-extensions


    【解决方案1】:

    源代码可用,因此您只需查看控制器:

    public function actionMovePersons()
    {
       if(isset($_POST['Person']['australia']))
       {
          foreach ($_POST['Person']['australia'] as $id)
          Person::model()->updateUserCountry($id, 14);
       }
    
       if(isset($_POST['Person']['newzealand']))
       {
          foreach ($_POST['Person']['newzealand'] as $id)
          Person::model()->updateUserCountry($id, 158);
       }
       Yii::app()->user->setFlash('saved',Yii::t('ui','Data successfully saved!'));
       $this->redirect(array('site/extension','view'=>'listbuilder'));
    }
    

    在模型中:

    public function updateUserCountry($id, $country_id)
    {
       $model=$this->findByPk($id);
       $model->country_id=$country_id;
       $model->update('country_id');
    }
    

    上面更新了数据库中的 Person 并设置了新的国家。如果我理解您的问题是正确的,您想创建新条目(可能在不同的表中)而不是更改现有条目。为此,您可以轻松修改updateUserCountry() 函数以创建新的Person 而不是更新它。

    【讨论】:

    • 谢谢...但是,我在哪里触发了我的小部件中的“Moveperson”操作?
    • @satya 周围的form 定义了调用哪个动作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多