Ticket #982 (closed defect: fixed)

Opened 15 months ago

Last modified 14 months ago

LocalKey considers and id of 0 to be empty

Reported by: wizhippo Owned by: romanb
Priority: major Milestone: 0.11.0
Component: Relations Version: 0.11.0
Severity: Keywords:
Cc: Has Test:
Status: Has Patch:

Description

If your table uses 0 as a key then the related class will not be loaded as Doctrine beleives it to be empty.

In Relation/LocalKey.php on line 49:

if (empty($id) ! $this->definitiontable?->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) {

if the id = 0 it is considered empty.

I don't believe this should be the case as 0 can be a valid id value.

see for a more details  http://groups.google.com/group/doctrine-user/browse_thread/thread/167407b0c187866d#

Change History

follow-up: ↓ 3   Changed 15 months ago by romanb

This can be fixed easily but it would be best to have a small testcase for this to prevent regression. Would you mind creating a small ticket testcase for the issue?

  Changed 15 months ago by romanb

  • milestone changed from 0.11.2 to 0.11.0

in reply to: ↑ 1   Changed 15 months ago by wizhippo

Replying to romanb:

This can be fixed easily but it would be best to have a small testcase for this to prevent regression. Would you mind creating a small ticket testcase for the issue?

I haven't made a test case before. I followed the instruction post on the side. I believe this is what you want. I have not tested it as i do not have a means to currently do so.

<?php
/*
 * Test to ensure LocalKey Relations allow 0 for id value
 */
class Doctrine_Sample_TestCase extends Doctrine_UnitTestCase
{
    public function prepareTables()
    {
        $this->tables[] = "MyModel";
        parent::prepareTables();
    }

    public function prepareData()
    {
	  $this->myModelZero = new MyModel();
	  $this->myModelZero->id = 0;
	  $this->myModelZero->patrentid = 0;
      $this->myModelZero->save();
	  
      $this->myModelOne = new MyModel();
	  $this->myModelOne->id = 1;
	  $this->myModelOne->patrentid = 0;
      $this->myModelOne->save();
	  
      $this->myModelTwo = new MyModel();
	  $this->myModelTwo->id = 2;
	  $this->myModelTwo->patrentid = 1;
      $this->myModelTwo->save();
    }

    public function testInit()
    {

    }

    public function testTest()
    {
		$this->assertEqual($this->myModelZero->id, 0);
		$this->assertEqual($this->myModelZero->patrentid, 0);
		$this->assertEqual($this->myModelZero->parent->id, 0);
		$this->assertEqual($this->myModelZero->parent->parentid, 0);		

        $this->assertEqual($this->myModelOne->id, 1);
		$this->assertEqual($this->myModelOne->patrentid, 0);
		$this->assertEqual($this->myModelOne->parent->id, 0);
		$this->assertEqual($this->myModelOne->parent->parentid, 0);	

        $this->assertEqual($this->myModelTwo->id, 2);
		$this->assertEqual($this->myModelTwo->patrentid, 1);
		$this->assertEqual($this->myModelTwo->parent->id, 1);
		$this->assertEqual($this->myModelTwo->parent->parentid, 0);	
   }
}

class MyModel extends Doctrine_Record
{
    public function setTableDefinition()
    {
    	$this->hasColumns(array(
							'id' => array(
                            	'type' => 'integer',
                            	'primary' => true,
								'notnull' => true
                        	),
							'parentid' => array(
                            	'type' => 'integer',
								'notnull' => true
                        	),
							));
    }

    public function setUp()
    {
        $this->hasOne('MyModel as parent', array('local' => 'parentid', 'foreign' => 'id'));
    }

}

  Changed 14 months ago by romanb

  • status changed from new to closed
  • resolution set to fixed

(In [4296]) Fixed #982.

Note: See TracTickets for help on using tickets.