gpt4 book ai didi

php - yii 中的多模型形式

转载 作者:IT王子 更新时间:2023-10-29 00:17:10 26 4
gpt4 key购买 nike

如何在 Yii 中创建多模型表单?我搜索了 Yii 的整个文档,但没有得到任何有趣的结果。有人可以给我一些方向或想法吗?任何帮助都将不胜感激。

最佳答案

根据我的经验,我得到了这个解决方案,并且可以快速理解

对于要收集的数据,您有两种模型。假设 PersonVehicle

第 1 步:设置用于输入表单的 Controller

在你的 Controller 中创建模型对象:

public function actionCreate() {

$Person = new Person;
$Vehicle = new Vehicle;

//.. see step nr.3

$this->render('create',array(
'Person'=>$Person,
'Vehicle'=>$Vehicle)
);
}

第 2 步:编写 View 文件

//..define form
echo CHtml::activeTextField($Person,'name');
echo CHtml::activeTextField($Person,'address');
// other fields..

echo CHtml::activeTextField($Vehicle,'type');
echo CHtml::activeTextField($Vehicle,'number');

//..enter other fields and end form

在你的 View 中放置一些标签和设计 ;)

第 3 步:在 $_POST 操作上编写 Controller

现在回到您的 Controller 并为 POST 操作编写功能

if (isset($_POST['Person']) && isset($_POST['Vehicle'])) {
$Person = $_POST['Person']; //dont forget to sanitize values
$Vehicle = $_POST['Vehicle']; //dont forget to sanitize values
/*
Do $Person->save() and $Vehicle->save() separately
OR
use Transaction module to save both (or save none on error)
http://www.yiiframework.com/doc/guide/1.1/en/database.dao#using-transactions
*/
}
else {
Yii::app()->user->setFlash('error','You must enter both data for Person and Vehicle');
// or just skip `else` block and put some form error box in the view file
}

关于php - yii 中的多模型形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com