Doctrine 1
  1. Doctrine 1
  2. DC-69

Linking between one to one relations with synchronizeWithArray fails

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 1.0.12, 1.1.4, 1.2.0-ALPHA1
    • Fix Version/s: 1.2.0-BETA1
    • Component/s: Relations
    • Labels:
      None
    • Environment:
      tested on
      MySQL
      Doctrine Version 1.0.11 Revision: 6380
      Doctrine Version 1.1.3 Revision: 6380
      Doctrine Version 1.1.4 Revision: 6409
      Doctrine Version 1.2

      Description

      Linking between one to one relations with synchronizeWithArray() ends up in an error message.

      Example:
      Group has one User
      User has one Group

      When I have a group-object and do the following:

      $group->synchronizeWithArray(array(
      	'User' => array(1)
      ));
      $group->save();
      

      I get the message: "Fatal error: Call to a member function identifier() on a non-object in [..]lib/Doctrine/Record.php on line 2327"

      Apparantly $this->_references[$alias] should be an collection, but in an one to one relation it is a record (which makes sense in my opinion).

        Activity

        Hide
        Marcus Häußler added a comment -

        I just realized that the whole linking process in one to one relations is not working.

        A simple

        $group->link('User', 1);
        

        ends up in <b>Fatal error</b>: Uncaught exception 'Doctrine_Exception' with message 'Add is not supported for User' in [..]/Doctrine/Access.php:201

        Because in Doctrine_Record the add-method is completely missing
        I hope it's just more or less a copy/past of Doctrine_Collection->add()

        Show
        Marcus Häußler added a comment - I just realized that the whole linking process in one to one relations is not working. A simple $group->link('User', 1); ends up in <b>Fatal error</b>: Uncaught exception 'Doctrine_Exception' with message 'Add is not supported for User' in [..] /Doctrine/Access.php:201 Because in Doctrine_Record the add-method is completely missing I hope it's just more or less a copy/past of Doctrine_Collection->add()
        Hide
        Marcus Häußler added a comment - - edited

        I have build a fix that works for me, but I did not tested it fully.

        Index: library/Doctrine/Doctrine/Record.php
        ===================================================================
        --- library/Doctrine/Doctrine/Record.php        (Revision 6462)
        +++ library/Doctrine/Doctrine/Record.php        (Arbeitskopie)
        @@ -2323,13 +2323,23 @@
                 }
        
                 if (isset($this->_references[$alias])) {
        -            foreach ($this->_references[$alias] as $k => $record) {
        -                if (in_array(current($record->identifier()), $ids) || empty($ids)) {
        -                    $this->_references[$alias]->remove($k);
        -                }
        -            }
        
        -            $this->_references[$alias]->takeSnapshot();
        +                       /*
        +                        * Fix for One To One Relations
        +                        */
        +               if ($this->_references[$alias] instanceof Doctrine_Record) {
        +                       if (in_array($this->_references[$alias]->identifier(), $ids) || empty($ids)) {
        +                               unset($this->_references[$alias]);
        +                       }
        +               } else {
        +                       foreach ($this->_references[$alias] as $k => $record) {
        +                               if (in_array(current($record->identifier()), $ids) || empty($ids)) {
        +                                       $this->_references[$alias]->remove($k);
        +                               }
        +                       }
        +                   $this->_references[$alias]->takeSnapshot();
        +               }
        +
                 }
        
                 if ( ! $this->exists() || $now === false) {
        @@ -2406,7 +2416,14 @@
                         ->execute();
        
                     foreach ($records as $record) {
        -                $this->$alias->add($record);
        +               /*
        +                * Fix for One To One Relations
        +                */
        +               if ($this->$alias instanceof Doctrine_Record) {
        +                       $this->$alias = $record;
        +               } else {
        +                       $this->$alias->add($record);
        +               }
                     }
        
        Show
        Marcus Häußler added a comment - - edited I have build a fix that works for me, but I did not tested it fully. Index: library/Doctrine/Doctrine/Record.php =================================================================== --- library/Doctrine/Doctrine/Record.php (Revision 6462) +++ library/Doctrine/Doctrine/Record.php (Arbeitskopie) @@ -2323,13 +2323,23 @@ } if (isset($this->_references[$alias])) { - foreach ($this->_references[$alias] as $k => $record) { - if (in_array(current($record->identifier()), $ids) || empty($ids)) { - $this->_references[$alias]->remove($k); - } - } - $this->_references[$alias]->takeSnapshot(); + /* + * Fix for One To One Relations + */ + if ($this->_references[$alias] instanceof Doctrine_Record) { + if (in_array($this->_references[$alias]->identifier(), $ids) || empty($ids)) { + unset($this->_references[$alias]); + } + } else { + foreach ($this->_references[$alias] as $k => $record) { + if (in_array(current($record->identifier()), $ids) || empty($ids)) { + $this->_references[$alias]->remove($k); + } + } + $this->_references[$alias]->takeSnapshot(); + } + } if ( ! $this->exists() || $now === false) { @@ -2406,7 +2416,14 @@ ->execute(); foreach ($records as $record) { - $this->$alias->add($record); + /* + * Fix for One To One Relations + */ + if ($this->$alias instanceof Doctrine_Record) { + $this->$alias = $record; + } else { + $this->$alias->add($record); + } }

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Marcus Häußler
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: