Php 是的,一种形式的多个模型实例
我们有两种型号Php 是的,一种形式的多个模型实例,php,forms,validation,yii,Php,Forms,Validation,Yii,我们有两种型号 class Base extends AbstractModel { public $id; public $name; public function tableName() { return 'table_base'; } } class ExtendBase extends AbstractModel { public $id; public $baseID; public functio
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))
}
公共函数actionSome()
{
$oBase=newbase();
$aExtendBase=array();//重要信息这是array
对于($i=1;$i isPost()){//受保护的自定义方法
if(isset($\u POST['Base'],$\u POST['extendebase'])//这里应该是特定的检查器,因为表单中有几个extendebase实例
{
//保存基模型
//保存每个扩展数据库模型
}
}
$this->render('formView',array('oBase'=>$oBase,'aExtendBase'=>$aExtendBaes))
}
所以问题是
请参见Yii WIKI上的解决方案 您可以创建一个模型(
CFormModel
),该模型不表示仅用于单个表单的ActiveRecord
我认为这是你继续工作的最佳方式(假设我正确理解了你的问题)
如果我是你,我会创建一个模型来表示这个表单的规则(就像我说的,
CFormModel
,而不是ActiveRecord
),然后在你的控制器中,获取提交的元素,手动为这两个类创建和/或更新对象。只有当我们有不同的模型时,它才会起作用。但我们有相同的,所以它在我们的情况下不起作用。