Doctrine 1
  1. Doctrine 1
  2. DC-464

[Inheritance] Couldn't save record with inheritance by a relationship if the record has no column modified (solution proposed)

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.2.1
    • Fix Version/s: None
    • Component/s: Inheritance
    • Labels:
      None

      Description

      Please consider the following TestCase

      Inheritance with no property setted, saved by relationship
      class Doctrine_Ticket_DC464_TestCase extends Doctrine_UnitTestCase
      {
          public function prepareTables()
          {
              $this->tables[] = 'Ticket_DC464_Entity';
              $this->tables[] = 'Ticket_DC464_Article';
              $this->tables[] = 'Ticket_DC464_User';
              $this->tables[] = 'Ticket_DC464_Group';
              parent::prepareTables();
          }
      
          public function testTest()
          {
              $User = new Ticket_DC464_User();
      
              $Article = new Ticket_DC464_Article();
              $Article->title = 'my first article';
              $Article->Author = $User;
      
              $Article->save();
      
              $UserFetched = Doctrine::getTable('Ticket_DC464_User')->find(1);
              $this->assertTrue($UserFetched instanceof Ticket_DC464_User);
             //$this->assertEqual($UserFetched->type, 1);
      
      
          }
      }
      
      
      
      class Ticket_DC464_Entity extends Doctrine_Record
      {
          public function setTableDefinition()
          {
              $this->hasColumn('id', 'integer', 11, array('autoincrement' => true, 'primary' => true));
              $this->hasColumn('facultative_field', 'string', 255);
              $this->hasColumn('type', 'string', 255);
      
              $this->setSubClasses(array(
                   'Ticket_DC464_User' =>  array('type' => 1),
                   'Ticket_DC464_Group' => array('type' => 2),
              ));
      
              $this->option('type', 'InnoDB');
          }
      
          public function setUp()
          {
          }
      }
      
      class Ticket_DC464_User extends Ticket_DC464_Entity { }
      class Ticket_DC464_Group extends Ticket_DC464_Entity { }
      
      class Ticket_DC464_Article extends Doctrine_Record
      {
          public function setTableDefinition()
          {
              $this->hasColumn('id', 'integer', 11, array('autoincrement' => true, 'primary' => true));
              $this->hasColumn('title', 'string', 30);
              $this->hasColumn('author_id', 'integer', 11);
      
              $this->option('type', 'InnoDB');
          }
      
          public function setUp()
          {
              $this->hasOne('Ticket_DC464_User as Author', array(
                   'local' => 'author_id',
                   'foreign' => 'id'));
          }
      }
      

      Notice that in the test, the $User instance has no property setted (in fact in this test, the "useless_field" in the model is... useless).

      This test fails (the $User object is not saved) because the method UnitOfWork->saveRelatedLocalKeys() - called in saveGraph(), itself called by $Article->save() - returns false because no column has been modified (line 395).

      To fix this, the Record->assignInheritanceValues() should be executed in top of isModified(), or for each related object in UnitOfWork->saveRelatedLocalKeys() (and probably UnitOfWork->saveRelatedForeignKeys() too).

      I've attached the test.

      1. DC464TestCase.php
        3 kB
        Colin Darie
      2. Record.php.patch
        0.5 kB
        Colin Darie

        Activity

        Hide
        Colin Darie added a comment -

        Fix test with the right prefixes

        Show
        Colin Darie added a comment - Fix test with the right prefixes
        Hide
        Jonathan H. Wage added a comment -

        Do you have a patch for the issue too?

        Show
        Jonathan H. Wage added a comment - Do you have a patch for the issue too?
        Hide
        Colin Darie added a comment -

        Hi,

        no I doesn't have a patch for this, because I was not sure what the best way to solve this.
        However, patching the isModified() method is maybe the safest way, with a simple $this->assignInheritanceValues(); at top (around line 2100).

        Show
        Colin Darie added a comment - Hi, no I doesn't have a patch for this, because I was not sure what the best way to solve this. However, patching the isModified() method is maybe the safest way, with a simple $this->assignInheritanceValues(); at top (around line 2100).
        Hide
        Jonathan H. Wage added a comment -

        Can you provide a patch for that change then so I can see it and test it? Thanks, Jon

        Show
        Jonathan H. Wage added a comment - Can you provide a patch for that change then so I can see it and test it? Thanks, Jon
        Hide
        Colin Darie added a comment -

        I've attached the patch.

        Once again, this patch fix the issue but there is maybe a better way to fix it to avoid the assignInheritanceValues() at each time.

        Thanks.

        Show
        Colin Darie added a comment - I've attached the patch. Once again, this patch fix the issue but there is maybe a better way to fix it to avoid the assignInheritanceValues() at each time. Thanks.

          People

          • Assignee:
            Roman S. Borschel
            Reporter:
            Colin Darie
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: