Ticket #889 (closed defect: fixed)
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