Question

I'm trying to build a model for content pages on my site, and i gave it a self reference named 'Related Page'. Now if i build my models and try to create a new page in the admin generator everything works fine. If i give the page a related page (or more) it works fine too, but if i go to the other page and try to edit the references symfony crashes:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2-2' for key 'PRIMARY'

I checked it in phpmyadmin and noticed that in the association table i have 2 ids (id1 and id2) and only the first id is a link:

(sorry I couldn't paste a link)

I tried to give an 'id' field to the association table so i had 3 fields (id, id1, id2), and i saw that when I update the relations in the other page (page2 to page1 and page2 to page3), it is creating the relations for that page (page2 to page1 and page3) and it is deleting the old relations (it was page1 to page2 and page1 to page3) and the final relations i have in the association table is page1 to page 1 (???) page 2 to page1 and page2 to page3.

My models are:

Page:
//...
      relations:
        RelatedPages:
          class: Page
          local: id1
          foreign: id2
          refClass: RelatedPage
          equal: true

RelatedPage:
  columns:
    id1:
      type: integer(4)
      primary: true
      unsigned: true
      notnull: true
    id2:
      type: integer(4)
      primary: true
      unsigned: true
      notnull: true

I'm using the symfony admin generator. Do I have to write some code to handle this, or am I doing something wrong?

I checked the doctrine documentation: link text

and it seems that everything is OK.

Was it helpful?

Solution 2

Okay, it was some kind of Doctrine bug:

link text

The solution is there.

OTHER TIPS

You need to add onDelete: CASCADE to the RelatedPages relation.

Here's the Symfony bug: http://trac.symfony-project.org/ticket/6273

edem solution is not working for me.
This is my solution.
Write this in lib/model/RelatedPage.class.php

class RelatedPage extends BaseRelatedPage
{


    public function save(Doctrine_Connection $conn = null)
    {
        if ($this->isNew() !== true && $this->getId1() == $this->getId2()) {
            return;
        }//end if

        return parent::save($conn);
    }//end save()

}//end class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top