我是Yii 1.1的新人 我想将一个对象保存在两个单独的表中,请检查我的代码。
$policyMaping = new PolicyRules();
$policyMaping->policy_rules_master_id = $data['policy']['id'];
$policyMaping->rules_id = $newRule->id;
$policyMaping->studio_id = $studio_id;
$policyMaping->policy_type = $data['policy']['name'];
$policyMaping->duration_type = $data['policy']['duration_type'];
$policyMaping->policy_value = $policy;
$policyMaping->created_date = gmdate('Y-m-d H:i:s');
$policyMaping->save();
$policyMaping = new PolicyRulesLog();
//$policyMaping->policy_rules_master_id = $data['policy']['id'];
//$policyMaping->rules_id = $newRule->id;
//$policyMaping->studio_id = $studio_id;
//$policyMaping->policy_type = $data['policy']['name'];
//$policyMaping->duration_type = $data['policy']['duration_type'];
//$policyMaping->policy_value = $policy;
//$policyMaping->created_date = gmdate('Y-m-d H:i:s');
$policyMaping->user_id = Yii::app()->user->id;
$policyMaping->save();
我已经发布了我的需求代码。有两个表PolicyRules
和PolicyRulesLog
两个表列相同,除了user_id
我想在我的两个表中保存请求的数据。我不想在单独的变量中编写相同的代码。请帮帮我。
答案 0 :(得分:0)
Yes, you can save same data into different tables. I have modified your code. Check the code below and let me know if it works for you.
$policyMaping = new PolicyRules();
$policyMaping->policy_rules_master_id = $data['policy']['id'];
$policyMaping->rules_id = $newRule->id;
$policyMaping->studio_id = $studio_id;
$policyMaping->policy_type = $data['policy']['name'];
$policyMaping->duration_type = $data['policy']['duration_type'];
$policyMaping->policy_value = $policy;
$policyMaping->created_date = gmdate('Y-m-d H:i:s');
$policyMaping->save(FALSE); // use false parameter to disable validation otherwise just use save only
$policyMapingLog = new PolicyRulesLog();
$policyMapingLog->policy_rules_master_id = $data['policy']['id'];
$policyMapingLog->rules_id = $newRule->id;
$policyMapingLog->studio_id = $studio_id;
$policyMapingLog->policy_type = $data['policy']['name'];
$policyMapingLog->duration_type = $data['policy']['duration_type'];
$policyMapingLog->policy_value = $policy;
$policyMapingLog->created_date = gmdate('Y-m-d H:i:s');
$policyMapingLog->user_id = Yii::app()->user->id;
$policyMapingLog->save(FALSE);
In Short you can also use...
$policyMaping = new PolicyRules();
$policyMaping->setAttributes($data);
$policyMaping->rules_id = $newRule->id;
$policyMaping->studio_id = $studio_id;
$policyMaping->policy_value = $policy;
$policyMaping->created_date = gmdate('Y-m-d H:i:s');
$policyMaping->save(FALSE);
$policyMapingLog = new PolicyRulesLog();
$policyMapingLog->setAttributes($data);
$policyMapingLog->rules_id = $newRule->id;
$policyMapingLog->studio_id = $studio_id;
$policyMapingLog->policy_value = $policy;
$policyMapingLog->created_date = gmdate('Y-m-d H:i:s');
$policyMapingLog->user_id = Yii::app()->user->id;
$policyMapingLog->save(FALSE);