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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$15
Phantom INSERT after calling Doctrine_Record::delete()

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();

}

}

peter | 01/25/13 at 12:41pm Edit
Tutorial: How to assign prize money

Previous versions of this question: 01/30/13 at 11:20am

(2) Responses

Note: peter felt their question was unanswered, so we granted them a refund.

Note: peter requested a refund. They offered this explanation:

"No valid answer to the problem has been provided. After spending much time scouting around, the answer is in http://stackoverflow.com/questions/11024251/doctrine-prevent-new-insert-in-related-tables and http://doctrine.readthedocs.org/en/latest/en/manual/working-with-models.html#clearing-related-records - I had to add in calls to $question->clearRelated(); to make it work."

If no one challenges a refund request, then they are automatically granted and proccessed after 48 hours. Admins of this site only review refund requests if someone challenges the request. If you are curious about how we handled previous refund requests, you may read over all refund requests and their challenges.

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/25/13
    3:05pm
    Luis Cordova says:

    your mapping has an ON DELETE SET NULL?

  • avatar
    Last edited:
    01/30/13
    11:19am
    peter says:

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

This question has expired.





Current status of this question: Refunded



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.