Doctrine 1
  1. Doctrine 1
  2. DC-953

Doctrine fails when using link() on OneToMany because of failing save

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.2.3
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      PHP 5.2.10, MySQL database connection

      Description

      I have continually run into a very particular bug when using OneToMany relationships between Doctrine tables. When attempting to call "link()" to generate a relationship between records in related tables, Doctrine attempts to set the ID of the "one" portion of the record to 0, then save it.

      This is best demonstrated by the sample YML and PHP that I have attached. It establishes a OneToMany relationship where the "one" table has another foreign key constraint. This causes the DB to trigger a foreign key constraint error when Doctrine tries to set the ID to 0, making the error easier to see.

      From looking into the relevant sections of the codebase, the following appears to be happening:

      • Calling "link" properly adds the relationship to the "many" record.
      • Calling "save" triggers the saving of the "many" record and all associated records.
      • With the "one" record now listed as an associated record, its own "save" function is called as a result.
      • Its "save" function works properly, except that for some reason, the ID key is not present in the $_data internal array (but oddly is present in the $_id array, allowing it to be returned in other places).
      • Seeing a difference, Doctrine attempts to execute an "UPDATE" query setting the ID to 0.

      I've made it this far in looking into it, but for the life of me I can't figure out what is triggering the identifier being reset in this case. I should note, however, that it happens consistently in every such situation on every server I've tested it on.

      1. test.php
        0.4 kB
        Buster Neece
      2. test.yml
        1 kB
        Buster Neece

        Activity

        Hide
        Buster Neece added a comment -

        Further research into the issue has revealed the exact area where the problem is being caused:

        Doctrine_Collection (272): Function "setReference", called from Doctrine_Relation_ForeignKey (80).

        For each of the elements in the collection (in this case, the related items), that function is setting the "reference field" value to the record being related to. Apparently, it's getting the field names confused, because it's overwriting "id" with a reference to the entire related object, which has a different ID.

        I can't tell if this is only an issue when both the relation tables use the same identifier ("id"), but this is surely common enough to warrant a fix.

        Show
        Buster Neece added a comment - Further research into the issue has revealed the exact area where the problem is being caused: Doctrine_Collection (272): Function "setReference", called from Doctrine_Relation_ForeignKey (80). For each of the elements in the collection (in this case, the related items), that function is setting the "reference field" value to the record being related to. Apparently, it's getting the field names confused, because it's overwriting "id" with a reference to the entire related object, which has a different ID. I can't tell if this is only an issue when both the relation tables use the same identifier ("id"), but this is surely common enough to warrant a fix.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Buster Neece
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: