public static function updateQuestions($parent_id, $text_array, $type_array, $id_array = null, $optionalCustomAnswer_array = null) { $q_table = new ITechTable(array('name' => 'evaluation_question')); $a_table = new ITechTable(array('name' => 'evaluation_custom_answers')); $existing = $q_table->fetchAll($q_table->select()->where("evaluation_id = {$parent_id}")); foreach ($text_array as $i => $q) { if (!empty($text_array[$i]) && !empty($type_array[$i])) { // find row $q_row = null; if ($id_array[$i] && $id_array[$i] != -1) { $q_row = $q_table->find($id_array[$i])->current(); } if ($q_row == null) { $q_row = $q_table->createRow(); } // populate and save $q_row->evaluation_id = $parent_id; $q_row->question_text = $text_array[$i]; $q_row->question_type = $type_array[$i]; $q_row->weight = $i; $id = $q_row->save(); if ($id && isset($optionalCustomAnswer_array[$i]) && is_array($optionalCustomAnswer_array[$i]) && count($optionalCustomAnswer_array)) { $num_rows_deleted = $a_table->delete("evaluation_id={$parent_id} and question_id={$id}"); // remove old answers linked to the evaluation, not sure of a proper way to reuse the rows, seek() seems broken in this version of zend which makes it rather difficult, #TODO foreach ($optionalCustomAnswer_array[$i] as $answer) { if (trim($answer) === '') { continue; } $a_row = $a_table->createRow(); $a_row->evaluation_id = $parent_id; $a_row->question_id = $id; $a_row->answer_phrase = $answer; $a_row->save(); } } } else { // delete (empty text, and an id, should delete this question) if ($id_array[$i] && $id_array[$i] != -1) { $q_table->find($id_array[$i])->current()->delete(); } } } return true; }