Ask your Symfony questions! Pay money and get answers fast! (more info)

Phantom INSERT after calling Doctrine_Record::delete() Symfony

  • REFUNDED

I have a set of Doctrine objects which I convert to JSON and pass to the client side, and then have to update with the response.

All works well apart from the code to remove an answer from a question. instead of just deleting the answer, Doctrine deletes it and then triggers an INSERT with no values:


4231 Query START TRANSACTION
4231 Query DELETE FROM poll_question_answer WHERE id = '293'
4231 Query INSERT INTO poll_question_answer (id, poll_question_id, answer, colour, size, poll_response_answer_total, priority) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL)
4231 Query rollback


The same thing happens when I try and delete a question:

4240 Query START TRANSACTION
4240 Query DELETE FROM poll_question WHERE id = '68'
4240 Query INSERT INTO poll_question (id, poll_id, question, poll_question_answer_total, answer_size, priority) VALUES (NULL, NULL, NULL, NULL, NULL, NULL)
4240 Query rollback



Code follows. The object hierarchy is: Poll has_many PollQuestion, which each have_many PollQuestionAnswer


class PollForm extends BasePollForm
{

// ...

protected function doSave($con = null)
{
/*
The JSON coming in looks like:
[
{
"id": 212,
"question": "Where does the rain fall in Spain?",,
"width": "full"
"answers": [
{
"id": 212,
"answer": "on the hills",
"colour": "cyan"
},
{
"id": 213,
"answer": "on the plains",
"colour": "pink"
},
{
"id": "",
"answer": "A new answer with no ID",
"colour": "pink"
}
]
},
{
"id": 323,
"question": "Where does the rain fall in Britain?",,
"width": "half"
"answers": [
{
"id": 323,
"answer": "Everwhere",
"colour": "green"
},
{
"id": 324,
"answer": "Nowhere",
"colour": "green"
}
]
}]
*/
parent::doSave($con);

$qa_data = json_decode($this->getValue('poll_json'));

// Remove deleted questions/answers
/* @var PollQuestion $question */
foreach ($this->getObject()->Questions as $question) {
$found = false;

foreach ($qa_data as $form_question) {
// If it's a new question, skip on
if ($form_question->id == '') {
continue;
}

if ($question->getId() == $form_question->id) {
// this question (still) exists, so do the same checking for answers
/* @var PollQuestionAnswer $answer */
foreach ($question->Answers as $answer) {
$aFound = false;

foreach ($form_question->answers as $form_answer) {
if ($answer->getId() == $form_answer->id) {
$aFound = true;
break;
}
}

if (!$aFound) {
$answer->delete();
}
}

$found = true;
break;
}
}

if (!$found) {
$question->delete();
}
}

// ... code here to update changes

$this->getObject()->save();

}

}

Answers (1)

2013-01-25

Luis Cordova answers:

your mapping has an ON DELETE SET NULL?


peter comments:

Not for any of these tables - checked in Doctrine's schema.yml, generated SQL and the database.