[Doctrine] #1035: Creating Tree does not set reference keys

0 views
Skip to first unread message

Doctrine

unread,
May 13, 2008, 3:26:26 AM5/13/08
to doctri...@googlegroups.com
#1035: Creating Tree does not set reference keys
-----------------------+----------------------------------------------------
Reporter: sigma | Owner: romanb
Type: defect | Status: new
Priority: minor | Milestone: 0.11.2
Component: NestedSet | Version: 0.11
Keywords: |
-----------------------+----------------------------------------------------
Using Doctrine 0.10.4

First of all, I'm pretty inexperienced with nested set like doctrine uses
them. So I'm not quite sure if this is really a bug...

Here are my model classes:

{{{
class Item extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('item');
$this->hasColumn('id', 'integer', 10, array('notnull' => true,
'primary' => true,
'unsigned' > true,
'autoincrement' =>
true));
$this->hasColumn('name', 'string' , 40 , array ('notnull' =>
true));
}

public function setUp() {
$this->hasOne('ItemTree', array('foreign' => 'item_id',
'local' => 'id'));
}
}

class ItemTree extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('item_tree');
$this->hasColumn('id', 'integer', 10, array('notnull' => true,
'primary' => true,
'unsigned' > true,
'autoincrement' =>
true));
$this->hasColumn('item_id' , 'integer' , 10 , array ('notnull' =>
true));
$options = array('hasManyRoots' => true, 'rootColumnName' =>
'root_id');
$this->actAs('NestedSet', $options);
}

public function setUp() {
$this->hasMany('Item', array('local' => 'item_id',
'foreign' => 'id'));
}
}
}}}

My script for building the tree:

{{{
// creating root
$item = new Item();
$item->name = 'Root';

$rootNode = new ItemTree();
$rootNode->Item[] = $item;

$tree = $conn->getTable('ItemTree')->getTree();
$tree->createRoot( $rootNode );

for ( $i=0; $i<5; $i++ ) {
$item = new Item();
$item->name = 'Entry at '.$i.' - 1';

$node = new ItemTree();
$node->Item[] = $item;
$node->getNode()->insertAsFirstChildOf( $rootNode );

for ( $j=0; $j<3; $j++ ) {
$innerItem = new Item();
$innerItem->name = 'Entry at '.$i.' - 2 - '.$j;

$nextNode = new ItemTree();
$nextNode->Item[] = $innerItem;
$nextNode->getNode()->insertAsFirstChildOf( $node );
}
}
}}}

After a built the tree I took a look into the database table item_tree and
saw that everything looks good beside my foreign key to the column
'item_id'. Doctrine didn't set this relation although it is defined in the
model class. Why? Is it a bug or it this the way it should be?

Obviously there is a one-to-one relation between item.id and item_tree.id,
right?!

--
Ticket URL: <http://trac.phpdoctrine.org/ticket/1035>
Doctrine <http://www.phpdoctrine.org>
PHP Doctrine Object Relational Mapper

Doctrine

unread,
May 13, 2008, 3:27:30 AM5/13/08
to doctri...@googlegroups.com
#1035: Creating Tree does not set reference keys
------------------------+---------------------------------------------------
Reporter: sigma | Owner: romanb
Type: defect | Status: new
Priority: minor | Milestone: 0.11.0
Component: NestedSet | Version: 0.11
Resolution: | Keywords:
------------------------+---------------------------------------------------
Changes (by sigma):

* milestone: 0.11.2 => 0.11.0

--
Ticket URL: <http://trac.phpdoctrine.org/ticket/1035#comment:1>

Doctrine

unread,
May 13, 2008, 3:43:02 AM5/13/08
to doctri...@googlegroups.com
#1035: Creating Tree does not set reference keys
------------------------+---------------------------------------------------
Reporter: sigma | Owner: romanb
Type: defect | Status: new
Priority: minor | Milestone: 0.11.0
Component: NestedSet | Version: 0.11
Resolution: | Keywords:
------------------------+---------------------------------------------------
Comment (by romanb):

First of all the association can't work that way. You specified a hasMany
from ItemTree to Item even though ItemTree has the foreign key. I assume
this should be a 1-1 as you said? Then you need to use hasOne on both
ends. So each node can only have one item associated with it. In addition
you might need to refresh the nodes when you do multiple inserts in a row
like, otherwise the nodes get out of synch and you get a corrupt tree.
This is not ideal and will hopefully improve in the future. I would also
rename ItemTree to ItemNode. Makes more sense to me. Try the code below
(after fixing the relation):


{{{
// creating root
$item = new Item();
$item->name = 'Root';

$rootNode = new ItemNode();
$rootNode->Item = $item;

$tree = $conn->getTable('ItemNode')->getTree();
$tree->createRoot( $rootNode );

for ( $i=0; $i<5; $i++ ) {
$item = new Item();
$item->name = 'Entry at '.$i.' - 1';

$node = new ItemNode();
$node->Item = $item;
$rootNode->refresh();
$node->getNode()->insertAsFirstChildOf( $rootNode );

for ( $j=0; $j<3; $j++ ) {
$innerItem = new Item();
$innerItem->name = 'Entry at '.$i.' - 2 - '.$j;

$nextNode = new ItemNode();
$nextNode->Item = $innerItem;
$node->refresh();
$nextNode->getNode()->insertAsFirstChildOf( $node );
}

}}}

--
Ticket URL: <http://trac.phpdoctrine.org/ticket/1035#comment:2>

Doctrine

unread,
May 16, 2008, 9:44:25 PM5/16/08
to doctri...@googlegroups.com
#1035: Creating Tree does not set reference keys
------------------------+---------------------------------------------------
Reporter: sigma | Owner: romanb
Type: defect | Status: closed
Priority: minor | Milestone: 0.11.0
Component: NestedSet | Version: 0.11
Resolution: invalid | Keywords:
------------------------+---------------------------------------------------
Changes (by jwage):

* status: new => closed
* resolution: => invalid

--
Ticket URL: <http://trac.phpdoctrine.org/ticket/1035#comment:3>
Reply all
Reply to author
Forward
0 new messages