Ticket #889 (closed defect: fixed)

Opened 16 months ago

Last modified 14 months ago

Self-Referencing utilzing join-table

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

Description

Using 0.10.3 version of Doctrine does not export self-references correctly. When creating a parent/child many-many relationship with the same object the second hasMany statement does not get parsed correctly. Below is the code utilized:

Self-Referencing Table

<?php

class BasePBO extends Doctrine_Record
{
    public function setTableDefinition()
    {
        
		// set Table Name
		$this->setTableName('PBO');
		
		// set table type 
		$this->option('type', 'INNODB');
		
		// set character set
        $this->option('charset', 'utf8');
		
		// id
		$this->hasColumn('id', 'integer', 10, array('primary' => true, 'unsigned' => true, 'autoincrement' => true));
		
		// table_name
		$this->hasColumn('table_name', 'string', 100, array('notnull' =>true, 'notblank' =>true, 'unique' => true ) );

		

    }

	public function setUp() 
	{
		// PBO_Relationship child_id 
		$this->hasMany('PBO as Parent', array('local' => 'child_id', 'foreign' => 'parent_id', 'refClass' => 'PBO_Relationship') );
		
		// PBO_Relationship parent_id 
		$this->hasMany('PBO as Child', array('local' => 'parent_id', 'foreign' => 'child_id', 'refClass' => 'PBO_Relationship') );
				
	}

}

?>

Join Table

<?php

class BasePBO_Relationship extends Doctrine_Record
{
    public function setTableDefinition()
    {
        
		// set Table Name
		$this->setTableName('PBO_Relationship');
		
		// set table type 
		$this->option('type', 'INNODB');
		
		// set character set
        $this->option('charset', 'utf8');
		
		// parent_id
		$this->hasColumn('parent_id', 'integer', 10, array( 'primary' => true, 'unsigned' => true ));
		
		// child_id
		$this->hasColumn('child_id', 'integer', 10, array( 'primary' => true, 'unsigned' => true ));

    }

}

?>

Build/Create Script

<?php

//require the base Doctrine class
require_once('../../doctrine/lib/Doctrine.php');

//register the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));

//set up a connection
Doctrine_Manager::connection('mysql://pumpdb:pumpdb@pumpdev.pumprt.com/Doctrine_Framework');

//export the classes
Doctrine::createTablesFromModels( './' );

?>

This fails to create the child_id foreign key restraint on the PBO_Relationship Table.

by echoing the queries you can see the add constraint for the parent_id but not the child_id.

<?php

//require the base Doctrine class
require_once('../../doctrine/lib/Doctrine.php');

//register the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));

//set up a connection
Doctrine_Manager::connection('mysql://pumpdb:pumpdb@pumpdev.pumprt.com/Doctrine_Framework');

$queries = Doctrine::generateSqlFromModels('./');

echo $queries;
?>

Also when trying to utilize the $pbo->Child query after obtaining some PBOs i get the following error:

[19-Mar-2008 12:48:22] PHP Notice: Undefined index: pbo in /Applications/MAMP/htdocs/PUMP_Framework/doctrine/lib/Doctrine/Hydrator.php on line 289

Any help on this issue would be appreciated, if i have some invalid syntax I apologize in advance, Thanks

Attachments

SelfRelation_TestCase.php (3.8 KB) - added by mdbitz 16 months ago.
Self-Relationship Many-Many Test Case

Change History

Changed 16 months ago by jwage

Can you provide your test above as a failing test case?

 http://trac.phpdoctrine.org/wiki/TestCases

Changed 16 months ago by mdbitz

Self-Relationship Many-Many Test Case

Changed 16 months ago by mdbitz

I have attached a Test Case that incorporates my Modules.

I am unfamiliar with some of the notions and if possible you might want to look over the Test Case to make sure it is valid.

There are 3 Tests included:

testManyTreeRelationWithSelfRelation_Children:

verify that the Children foreign key is created and that the PBO->Children call returns a PBO.

testManyTreeRelationWithSelfRelation_Parents:

verify that the Parents foreign key is created and that the PBO->Parents call returns a PBO.

testInitData:

verify that the Parents and Children calls will populate the join table.

Changed 15 months ago by jwage

Added coverage for this in r4181

Changed 14 months ago by jwage

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

(In [4313]) fixes #889

Changed 14 months ago by jwage

Your test was slightly invalid. The foreign keys for the refClass will not be set unless you define the relationships on the refClass itself. I have updated the test case so it passes and sets the relationships properly.

Note: See TracTickets for help on using tickets.