![](/img/trans.png)
[英]How to Store result of a HAS_MANY RELATION database query in yii?
[英]How to save data in the MySql tables with HAS_MANY relation by recursive way in Yii 1.7
通過一個插入查詢無法將數據插入到多個表中
您必須使用事務來執行許多插入
public function saveJsonData()
{
/**
* parsing your json data
*/
$transaction = Yii::app()->db->beginTransaction();
try {
$company = new Company();
$company->setAttributes($company_data);
if (!$company->save()) {
throw new Exception('some exception');
}
foreach ($otherCompanyRelatedDataList as $data) {
$otherCompanyData = new OtherCompanyData();
$otherCompanyData->setAttributes($data);
if (!$otherCompanyData->save()){
throw new Exception('some exception');
}
}
/**
* save other data
*/
$transaction->commit();
} catch (Exception $e) {
$transaction->rollback();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.