/** * Update answers from array. * * Method changes only state of the object you must * call save() if you want to push changes to database. * * @param array $answers * @return Poll_Question */ public function updateAnswers(array $answers) { $update = array(); $new = array(); foreach ($answers as $answer) { if (isset($answer['id'])) { unset($answer['responses']); $update[$answer['id']] = $answer; } else { $new[] = $answer; } } foreach ($this->getAnswers() as $answer) { $id = $answer->getId(); if (!isset($update[$id])) { $this->_answersToDelete[] = $id; } else { $answer->setValues($update[$id]); } } foreach ($new as $data) { $answer = new Poll_Answer(); $answer->setValues($data); $answer->setQuestionId($this->getId()); $this->getAnswers()->append($answer); } return $this; }