开发者

Doctrine Self Referencing is not working in Symfony 1.4 when updating or deleting

开发者 https://www.devze.com 2023-01-20 23:33 出处:网络
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 everythin

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 constrai开发者_如何学运维nt 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.


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

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


Okay, it was some kind of Doctrine bug:

link text

The solution is there.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消