JS 関数を使用して複数のレコードを挿入しようとしています。JS 関数は、挿入を実行する別の YII コントローラーのアクションを呼び出します。
データに対していくつかのアクションを実行し、変更されたデータを配列 post_data に保存する MySql テーブルからデータを読み取ります。後で、この配列を url 形式に変換し、パラメーターとして JS 関数に渡します。この関数には、挿入を実行するために別のコントローラーを呼び出す ajax があります。
2 つのテーブルに挿入しようとしています。最初に contact_details テーブルが挿入され、次に Engineer テーブルが挿入されるため、この ajax 呼び出しをシリアルに実行したいと考えています。つまり、前の挿入が完了してから次の挿入を開始したいのです。
私のコードは次のとおりです。
<script>
function call_migration_api(post_data)
{
//console.log( 'post data in js func = '+post_data);
//var db_data = post_data.serialize();//tried this, but not working.
var db_data = post_data;
$.ajax({
type: "POST",
url:"http://localhost/amica_migration/rapport/chs/Migrationapi/createengineer",
data: db_data,
success: function(server_response)
{
console.log(server_response)
}//end of success
});//end of $.ajax
}//end of function.
</script>
<?php
//reading all values in $post_data array.
foreach ( $post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
echo "<script>call_migration_api('$post_string');</script>";// Call to AJAX function.
?>
移行 API コード:
<?php
public function actionCreateEngineer()
{
$engineer_model=new Engineer();
$contactdetails_model=new ContactDetails();
//****** RETRIVING CONTACT DETAILS ***********
$contactdetails_model->address_line_1= $_POST['address_line_1'];
$contactdetails_model->town= $_POST['town'];
$contactdetails_model->postcode_s= $_POST['postcode_s'];
$contactdetails_model->postcode_e= $_POST['postcode_e'];
$contactdetails_model->telephone= $_POST['telephone'];
$contactdetails_model->email= $_POST['email'];
//********** SAVING CONTACT ***************
if($contactdetails_model->save())
{
echo "<br>Contact Model Saved";
$engineer_model->contact_details_id=$contactdetails_model->id;
$engineer_model->delivery_contact_details_id=$contactdetails_model->id;
//****** RETRIVING ENGINEER DETAILS ***********
$engineer_model->first_name=$_POST['first_name'];
$engineer_model->last_name=$_POST['last_name'];
$engineer_model->active=$_POST['active'];
$engineer_model->id=$_POST['engg_id'];
//****** SAVING ENGINEER DETAILS ***********
if($engineer_model->save())
{
echo "<br>Engineer saved";
}
else
{
echo "<br>Engineer not saved******** ENGG ERROR = ";
print_r($engineer_model->getErrors());
}
}//end of if contact save.
else
{
echo "<br>Contacts NOT SAVED ///////// CONTACT ERROR = ";
print_r($contactdetails_model->getErrors());
}
}//end of create engineer.
?>