【问题标题】:Yii, several instances of model in one formYii,一种形式的模型的几个实例
【发布时间】:2014-03-25 19:52:56
【问题描述】:

我们有两个模型

class Base extends AbstractModel
{
    public $id;
    public $name;

    public function tableName()
    {
        return 'table_base';
    }
}

class ExtendBase extends AbstractModel
{
    public $id;
    public $baseID;

    public function tableName()
    {
        return 'table_base_extend';
    }
}

我不会全部描述,只会描述主要的事情。

所以,现在我想创建将这些表单组合在一起的表单。并希望在行动中做这样的事情

public function actionSome ()
{
    $oBase = new Base();
    $aExtendBase = array(); // important this is array

    for ($i = 1; $i <= 3; $i++) {
        $aExtendBase[] = new Extend(); // fill in array, pre-create for form
    }

    if ($this->isPost()) { // protected custom method
        if (isset($_POST['Base'], $_POST['ExtendBase'])) // here should be specific checker because we have several ExtendBase instances in form
        {
            // save Base model
            // save each ExtendBase model
        }
    }

    $this->render('formView', array('oBase' => $oBase, 'aExtendBase' => $aExtendBaes))
}

所以问题是

  1. 如何在视图中创建这样的“组合”表单;
  2. 如何在 POST 操作后获取所有表单数据(我的意思是每个);

【问题讨论】:

    标签: php forms validation yii


    【解决方案1】:

    【讨论】:

    • 只有当我们有不同的模型时它才会起作用。但是我们有相同的,所以它在我们的情况下不起作用。
    【解决方案2】:

    您可以创建一个模型 (CFormModel),它不代表单个表单的 ActiveRecord

    我认为这将是您继续进行的最佳方式(假设我已正确理解您的问题)。

    如果我是你,我会创建一个模型来仅表示此表单的规则(就像我说的,CFormModel 的实例,而不是 ActiveRecord)然后,在你的控制器中,获取得到的元素为这两个类提交并手动创建和/或更新对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多