Welcome Guest, Not a member yet? Register   Sign In
need help with datamapper orm and nestedsets
#1

[eluser]kev[/eluser]
Hi all,

I just started using datamapper orm, which i found nice. I'm trying to use the nestedsets extenstion that comes with it. I have a categories table with the following field,

id
root_id
left_id
right_id
title

Below is the code im using to test it,

Code:
$tree = new Category();

  $tree->select_root(1);
  $tree->title = 'First Category';
  
  $tree->new_root();
  $tree->get_root();

  $tree->title = 'First Sub Category';

  $tree->new_first_child();

I get this in the table, see below :

id root_id left_id right_id
1 1 1 4
2 1 2 3

have the data been inserted correctly?

I have uploaded an attachment also.

Now that the data is in the database, I tried to ouput it using dump_tree, see code below :

Code:
echo $tree->dump_tree( array('title'), 'html' , FALSE );

But using this I get a blank screen.

This is my model :

Code:
<?php

class Category extends DataMapper {

var $table = 'categories';

    public $nestedsets = array(
        'name' => 'title'
    );
}

Can someome help me understand this? Sad
#2

[eluser]kev[/eluser]
Ok, i've been digging in the nestedsets.php file, and I found out that dump_tree has four parameters

Code:
dump_tree($object, $attributes = NULL, $type = 'array', $skip_root = TRUE)

I modified my code to read like below

Code:
$tree->dump_tree( $tree, array('title'), 'html' , TRUE ) ;

I've added a var_dump inside the dump_tree method.

When I var_dump the $object parameter, I get the Object category, good.

But when I var_dump the $attributes parameter, I still get the Object Category as output, is that normal? Am supose to get the Array title.. Am I wrong?

Can somebody please help?
#3

[eluser]kev[/eluser]
Anybody know what's causing this?

This is the new code im using..

Code:
$root = new Category();
  $root->get_root(); // get the root
  
  $obj = new Category();
  $obj->get_root();

  echo $root->dump_tree( $obj, array('title'), 'html' , true );

It's echoing Array instead of html output.
#4

[eluser]WanWizard[/eluser]
The first argument of any extension method is $object, which is passed to the method by Datamapper, and is the current object (the one you call the method on). So you don't need to pass that. If you do, all arguments shift one, and that's why dumping $attributes give you the object again.

The structure of your table data is fine, a root node (1-4) and one child node (2-3).

This should work fine:
Code:
$root = new Category();
$root->get_root(); // get the root
echo $root->dump_tree( array('title'), 'html' , true );

Something wrong with the tree definition, so that the query doesn't produce any results?

Check the queries using the profiler, or dump last_query() directly after dump_tree()...
#5

[eluser]kev[/eluser]
Thank you for your reply wanwizard!

I get something like this

1 "First Category" "first-category"
2 "First Sub Category" "first-sub-category"

Which i guess is ok?

Do you have any idea how to apply this to some sort of Unordered List Items?

Another question,

Code:
$tree = new Category();
$tree->select_root(1);
$tree->title = 'First Category';
$tree->new_root();

Why do we have to pass a value to select_root()?

If i'm creating a new root via a form, how do I manage that?

Thanks for your response again!

Kev
#6

[eluser]kev[/eluser]
I managed to apply this to an unorder list. see below

Code:
First Category
    First Sub Level

My question is, what code should I need if I want to keep insert childs? I want something like below:

Code:
First Category
    First Sub Level
        Another Sub Level
             Another Sub level again

I'm having a hard time with this.

And by the way, the root id always remain 1. Is that normal?

Thanks
#7

[eluser]WanWizard[/eluser]
Select "First Sub Level" and add "Another Sub Level" to it as a child.

The 'id' is just an abstract PK, it isn't used anywhere. As with all other records, the PK will remain the same for the entire lifecycle of the record. Why should it change?
#8

[eluser]kev[/eluser]
Do you have a sample code of how to keep adding child? it;s a bit confusing for me.

I mean, the root_id, should it always be 1 for every sub child in the table?
#9

[eluser]kev[/eluser]
id = 5
root_id = 1
left_id = 2
right_id = 3
title = First Sub Level

I'm confuse here on how to add a child to this. How do I select the first sublevel from my code? Can you help?
#10

[eluser]kev[/eluser]
Ok I think i'm finally close to this !!

Check my code below
Code:
$tree = new Category();
  $tree->select_root(1);
  $tree->get_root();
   $tree->get_by_id(5); // The id of the node i want to use as parent
  
// Here I assign the child passing the $tree object as a paramter to the new_first_child() method
  $child = new Category();
  $child->title = 'First Sub sub Level';
  $child->new_first_child($tree);

and this produce the result below :

Code:
First level
    First Sub Level
        First Sub sub Level

Am I correct with this code?




Theme © iAndrew 2016 - Forum software by © MyBB