我在我的表单中添加了行功能,而在更新表单上我需要更新旧数据,如果在表单中添加了任何新行,则在我的数据库中插入新数据我怎么能在YII中执行。 我的控制器代码是
if(isset($_POST['LeadTargetEducation']))
{
$target = new LeadTargetEducation;
$count= count($_POST['LeadTargetEducation']);
$target->attributes=$_POST['LeadTargetEducation'];
//the value for the foreign key 'groupid'
$lead_id = Yii::app()->db->createCommand()
->select('max(id) lead')
->from('lead_details')
->queryRow();
$val=$lead_id['lead'];
for($i=0;$i<$count;$i++)
{
$target->lead_id=$val;
$target->edu_id=$_POST['LeadTargetEducation'][$i]['edu_id'];
$target->country_id=$_POST['LeadTargetEducation'][$i]['country_id'];
$target->universty_id=$_POST['LeadTargetEducation'][$i]['universty_id'];
$target->academic_year=$_POST['LeadTargetEducation'][$i]['academic_year'];
$target->sub_edu=$_POST['LeadTargetEducation'][$i]['sub_edu'];
$target->course_id=$_POST['LeadTargetEducation'][$i]['course_id'];
$target->req_prior_target_edu=$_POST['LeadTargetEducation'][$i]['req_prior_target_edu'];
$target->add_course=$_POST['LeadTargetEducation'][$i]['add_course'];
$target->save();
}
}
答案 0 :(得分:1)
每次尝试保存新模型时都缺少新模型,这样做是为了防止更新同一记录:
for($i=0;$i<$count;$i++)
{
$target = new LeadTargetEducation(); // see this line
$target->lead_id=$val; // I issume this is not the primary key!
.
.
.
$target->save(); // and finally save it
}
答案 1 :(得分:0)
以下是beforesave方法的示例;您可以在模型中创建beforesave函数
function beforeSave(){
$tableB = new new LeadTargetEducation();
$tableB->edu_id = $this->edu_id;
more...
$tableB->save();
}