Doctrine 1
  1. Doctrine 1
  2. DC-319

getModified() returns object where ids are expected

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 1.1.7, 1.2.1
    • Fix Version/s: 1.2.2
    • Component/s: Record, Relations
    • Labels:
      None
    • Environment:
      PHP 5.2.10 on Ubuntu 9.10

      Description

      Assigning a new unsaved relation to an entity confuses getModified() to return records for values such as userId, that are expected to be ints.

      Here is a full example to reproduce:

      <?php
      require_once '_loader.php';
      
      $mgr = Doctrine_Manager::getInstance();
      $mgr->openConnection('....');
      
      class User extends Doctrine_Record
      {
          public function setTableDefinition()
          {
              $this->setTableName('t_users');
      
              $this->hasColumn('name', 'string', 32);
          }
      }
      
      class Post extends Doctrine_Record
      {
          public function setTableDefinition()
          {
              $this->setTableName('t_posts');
      
              $this->hasColumn('body', 'string', 256);
              $this->hasColumn('userId', 'integer');
          }
      
          public function setUp()
          {
              $this->hasOne('User', array(
                  'local'   => 'userId',
                  'foreign' => 'id'
              ));
          }
      }
      
      Doctrine::dropDatabases();
      Doctrine::createDatabases();
      Doctrine::createTablesFromArray(array('User', 'Post'));
      
      $joe = new User();
      $joe->name = 'Joe';
      $joe->save();
      
      $post = new Post();
      $post->body = 'Hi';
      $post->User = $joe;
      $post->save();
      
      $bill = new User();
      $bill->name = 'Bill';
      //$bill->save(); // If this is uncommented everything works fine
      
      $post->User = $bill;
      
      var_dump(' --- New --- ');
      var_dump($post->getModified());
      

      And instead of a id for userId I get this

      string(13) " --- New --- "
      array(1) {
        ["userId"]=>
        object(User)#37 (18) {
          ["_node:protected"]=>
          NULL
          ["_id:protected"]=>
          array(0) {
          }
          ["_data:protected"]=>
          array(2) {
            ["id"]=>
            object(Doctrine_Null)#4 (0) {
            }
            ["name"]=>
            string(4) "Bill"
          }
          ["_values:protected"]=>
          array(0) {
          }
          ["_state:protected"]=>
          int(2)
          ["_lastModified:protected"]=>
          array(0) {
          }
          ["_modified:protected"]=>
          array(1) {
            [0]=>
            string(4) "name"
          }
          ["_oldValues:protected"]=>
          array(1) {
            ["name"]=>
            NULL
          }
          ... trimmed ...
      

      expected

      string(13) " --- New --- "
      array(1) {
        ["userId"]=>
        string(1) "2"
      }
      

      Tested on 1.1 and 1.2 branches from svn. (same result)

        Activity

        Hide
        Jonathan H. Wage added a comment -

        This is the expected behavior. Because you haven't saved $bill yet, it is temporarily storing the reference to the object in the foreign key field.

        Show
        Jonathan H. Wage added a comment - This is the expected behavior. Because you haven't saved $bill yet, it is temporarily storing the reference to the object in the foreign key field.
        Hide
        Emil Vladev added a comment -

        But sholnd't it be NULL. userId is expected to be an int - not an object - doesn't feel right?!?

        Show
        Emil Vladev added a comment - But sholnd't it be NULL. userId is expected to be an int - not an object - doesn't feel right?!?
        Hide
        Jonathan H. Wage added a comment -

        Hmm. I guess it could go either way. If you haven't saved yet, then the property has changed, we just don't know the id yet. I could imagine someone wanting to know that an object was assigned to that foreign key but not saved yet. Either way, I don't think we can change the behavior in Doctrine 1.2 if people might be relying on it already.

        Show
        Jonathan H. Wage added a comment - Hmm. I guess it could go either way. If you haven't saved yet, then the property has changed, we just don't know the id yet. I could imagine someone wanting to know that an object was assigned to that foreign key but not saved yet. Either way, I don't think we can change the behavior in Doctrine 1.2 if people might be relying on it already.

          People

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

            Dates

            • Created:
              Updated:
              Resolved: