Doctrine 1
  1. Doctrine 1
  2. DC-684

After adding a Reference, the ForeignKeys in the DB are not updatet

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.2.2
    • Fix Version/s: None
    • Component/s: Record, Relations
    • Labels:
      None
    • Environment:
      Ubuntu 9.04, PHP 5.2.6, MySQL 5.1, Zend Framework

      Description

      After query a record from the DB, where a foreign key is null, and changing only the reference belonging to this foreign key, and only these reference no other value of the record, a save of the record doesn't lead to a change in the foreign key field.

      For example: I'll save a user record with some data of a regestration and give him the possibillity to add a city where he cames from later in his account settings, if no other user attribute is changed, the state of the user record is still CLEAN. The result is that I can access the user->City property correctly only as long as no refresh from the db is done. So the changing of the foreign key belonging to the city isn't persistent in the DB.

      Example Code:
      $user = new Model_User();
      $user->firstName = md5(rand());
      $user->save();

      $id = $user->id;
      unset($user);
      assert(! $user);

      $user = Model_UserTable::getInstance()->find($id);
      assert(!$user->relatedExists('City'));

      $user->City->name = 'Mainz';
      $user->save();
      unset($user);

      $user = Model_UserTable::getInstance()->find($id);
      $user->refresh(true);
      assert($user->City->name == 'Mainz');

      The last assert failse. I realized the problem while updating just a referenz using the fromArray() method like $user->City->fromArray(). The city would be saved correctly in DB but the foreign key in the user table would not be updated. After debugging I recognized that the $user is not replaced in UnitOfWork::saveGraph() because the $user is still in CLEAN state.

      Using " $user->coreSetRelated('Country', $user->Country);" or "$user->state(Doctrine_Record::STATE_DIRTY);" after "$user->City->name = 'Mainz'" would fix the prob in this case. As well as creating a new City instance an set $user->City with this instance like:

      $city = new Model_City();
      $city->name = 'Mainz';
      $user->City = $city;

      Also $user->isModified(true) is true. So I fixed the issues in my case by changing the state of my records to DIRTY if the "$this->state() == CLEAN && $user->isModified(true) == true" in the preSave() method of an abstract class that my Base classes are extending....

        Activity

        Hide
        Alexandre Ravey added a comment -

        Spend the last hour trying to get a workaround on this one.

        It apear to me that the solution is not easy. The object is modified while in STATE_LOCKED state, meaning that any state change in Record::_set() is not passed back to UnitOfWork::saveGraph() and therfor not saved in the database.

        Finaly I've managed to make a realy dirty fix, I'll attach it if it come handy to somebody.
        Keep in mind this is only a workaround and has NOT been tested. Use at your own risk...

        Show
        Alexandre Ravey added a comment - Spend the last hour trying to get a workaround on this one. It apear to me that the solution is not easy. The object is modified while in STATE_LOCKED state, meaning that any state change in Record::_set() is not passed back to UnitOfWork::saveGraph() and therfor not saved in the database. Finaly I've managed to make a realy dirty fix, I'll attach it if it come handy to somebody. Keep in mind this is only a workaround and has NOT been tested. Use at your own risk...
        Hide
        Alexandre Ravey added a comment - - edited

        Dirty fix, see comments.
        ! NOT tested thoroughly !

        Show
        Alexandre Ravey added a comment - - edited Dirty fix, see comments. ! NOT tested thoroughly !

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Lars Kosubek
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: