Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] NestedSet behaviour: appendTo() behaves as prependTo(). #513

Closed
temuri416 opened this issue Dec 16, 2015 · 12 comments
Closed

[BUG] NestedSet behaviour: appendTo() behaves as prependTo(). #513

temuri416 opened this issue Dec 16, 2015 · 12 comments

Comments

@temuri416
Copy link
Contributor

Hi,

I think there is a bug in inserting nodes to parent.

All newly inserted nodes are created as parent's first node (or, before any other sibling), regardless of whether appendTo() or prependTo() is used.

Here's how the rows look like as a result of appendTo() or prependTo():

image

As you can see node ID#16 was inserted last, however its lft value is lower than of its sibling ID#2 which was inserted before.

Thoughts?

Thanks!

@temuri416
Copy link
Contributor Author

@sergeyklay

Hi there,

Is there a chance that this gets addressed?

Thanks!

@sergeyklay
Copy link
Contributor

Hi, I'll try to deal with this in the near future

@temuri416
Copy link
Contributor Author

Awesome, most appreciated!

@sergeyklay
Copy link
Contributor

HI, Provide please script to reproduce (including table schema)

@temuri416
Copy link
Contributor Author

Sure, I can put something together.

But aren't you able to reproduce it with your own code and schema? Cuz that's exactly what I was doing. The same applies to #534.

Thanks!

@sergeyklay
Copy link
Contributor

Unfortunately I still haven't found enough time for this. But I'll try to deal at the weekend.
In any case, I will need to reproduce the issue.

@temuri416
Copy link
Contributor Author

ok, I'll try to make it easy for you :-) Will post code ASAP.

@temuri416
Copy link
Contributor Author

Hi @sergeyklay ,

Looks like this one is not fixed yet.

Tested with branch 2.0.x, git commit ID 0bbf7b5.

Table:

DROP TABLE IF EXISTS `tree`;
CREATE TABLE IF NOT EXISTS `tree` (
  `id` int(11) NOT NULL,
  `root` int(11) DEFAULT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `lft` int(11) DEFAULT NULL,
  `rgt` int(11) DEFAULT NULL,
  `level` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `tree` ADD PRIMARY KEY (`id`);
ALTER TABLE `tree` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Model class:

class Node extends \Phalcon\Mvc\Model
{
    public $title;

    public function initialize()
    {
        $this->setSource('tree');
        $this->addBehavior(new Phalcon\Mvc\Model\Behavior\NestedSet([
            'leftAttribute' => 'lft',
            'rightAttribute' => 'rgt',
            'levelAttribute' => 'level',
            'hasManyRoots' => true,
            'rootAttribute' => 'root',
        ]));
    }
}

The code:

$db = new \Phalcon\Db\Adapter\Pdo\Mysql([
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => 'letmein',
    'dbname' => 'test',
]);

$db->query('truncate table tree');
$db->query('ALTER TABLE tree AUTO_INCREMENT=1');

$this->di['db'] = $db;

$root = new Node;
$root->title = 'ROOT';
$root->saveNode();

// Add "A" as child of root
$node1=new Node;
$node1->title='A';
$node1->appendTo($root);

// Add "B" as last child of root - should be below A
$node2=new Node;
$node2->title='B';
$node2->appendTo($root);

// Add "C" as first child of root - should be above A
$node3=new Node;
$node3->title='C';
$node3->prependTo($root);

Observed result the DB:

image

Observed visual tree structure:

  \ROOT
    |
    +---C
    |
    +---B
    |
    +---A

Expected result the DB:

image

Expected result the DB:

  \ROOT
    |
    +---C
    |
    +---A
    |
    +---B

I'll look at #552 tomorrow.

Thanks!

@sergeyklay
Copy link
Contributor

I've realized how it should work.

$root = new Node;
$root->title = 'ROOT';
$root->saveNode();

// Add "A" as child of root
$node1 = new Node;
$node1->title = 'A';
$node1->appendTo($root);

// Add "B" as last child of root - should be below A
$node2 = new Node;
$node2->title = 'B';
$node2->appendTo($root);

// Add "C" as first child of root - should be above A
$node3 = new Node;
$node3->title = 'C';
$node3->prependTo($root);

This code should create such tree:
nested_set_appendpepend_2

Red numbers - PK
Blue numbers - Level
Black numbers - Left / Right

I fixed this issue in #552
See NestedSetTest::testShouldAddBelowAndAbove

@temuri416
Copy link
Contributor Author

I've tested with #552 and tree looks as expected.

@sergeyklay
Copy link
Contributor

Also check #535 and please #534

@sergeyklay
Copy link
Contributor

Fixed in 2.0.x

Fenikkusu pushed a commit to twistersfury/incubator that referenced this issue Dec 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants