salehforum
چهارشنبه 11 تیر 1393, 12:37 عصر
سلام
سوالی برای من پیش اومده در مورد نحوه ی استفاده ی دقیق از Transaction های Yii
می خواستم ببینم برای کد زیر آیا نحوه ی استفاده ی من از transaction ها صحیح هست؟ آیا اون hasError هایی که چک کردم به صورت صحیح استفاده شده یا اضافه هست؟ اصلا میشه هر دو رو با هم داشت؟
دقیقا روالش به چه صورت هست؟
با تشکر
$transaction = Yii::app()->db->beginTransaction();
try {
$question = new Question();
$question->user_id = $uid;
$question->question_date = $time;
$question->show_date = $time;
$question->title = $title;
$question->body = $body;
$question->category_id = $categories;
$question->anonymous = $anonymous;
$question->featured = $featured;
$question->polled = $polled;
$question->close_date = $time;
$question->feature_date = $time;
$question->save();
if ($question->hasErrors()) {
return ['success' => 102, 'error' => S()['failed']]; // question saving error
}
$qid = $question->id;
$user->saveCounters(
array(
'score' => $score,
'score_day' => $score,
'score_week' => $score,
'score_month' => $score,
'stock' => $stock,
)
);
if ($polled) { // if the question has poll...
$poll = new Poll();
$poll->question_id = $qid;
$poll->title = $title_poll;
$poll->max_vote = $max_number_vote_for_poll;
$poll->save();
if ($poll->hasErrors()) {
return ['success' => 106, 'error' => S()['failed']]; // question saving error
}
$poll_id = $poll->id;
$values = explode('-', $field_values);
foreach ($values as $value) { // add the fields to the fieldpoll table
$fieldPoll = new Fieldpoll();
$fieldPoll->poll_id = $poll_id;
$fieldPoll->value = $value;
}
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
return ['success' => 0, 'error' => S()['failed']]; // answer does not exist
}
سوالی برای من پیش اومده در مورد نحوه ی استفاده ی دقیق از Transaction های Yii
می خواستم ببینم برای کد زیر آیا نحوه ی استفاده ی من از transaction ها صحیح هست؟ آیا اون hasError هایی که چک کردم به صورت صحیح استفاده شده یا اضافه هست؟ اصلا میشه هر دو رو با هم داشت؟
دقیقا روالش به چه صورت هست؟
با تشکر
$transaction = Yii::app()->db->beginTransaction();
try {
$question = new Question();
$question->user_id = $uid;
$question->question_date = $time;
$question->show_date = $time;
$question->title = $title;
$question->body = $body;
$question->category_id = $categories;
$question->anonymous = $anonymous;
$question->featured = $featured;
$question->polled = $polled;
$question->close_date = $time;
$question->feature_date = $time;
$question->save();
if ($question->hasErrors()) {
return ['success' => 102, 'error' => S()['failed']]; // question saving error
}
$qid = $question->id;
$user->saveCounters(
array(
'score' => $score,
'score_day' => $score,
'score_week' => $score,
'score_month' => $score,
'stock' => $stock,
)
);
if ($polled) { // if the question has poll...
$poll = new Poll();
$poll->question_id = $qid;
$poll->title = $title_poll;
$poll->max_vote = $max_number_vote_for_poll;
$poll->save();
if ($poll->hasErrors()) {
return ['success' => 106, 'error' => S()['failed']]; // question saving error
}
$poll_id = $poll->id;
$values = explode('-', $field_values);
foreach ($values as $value) { // add the fields to the fieldpoll table
$fieldPoll = new Fieldpoll();
$fieldPoll->poll_id = $poll_id;
$fieldPoll->value = $value;
}
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
return ['success' => 0, 'error' => S()['failed']]; // answer does not exist
}