I've got a problem with the Tree behavior in an application I'm
maintaining. The version of cakephp is 1.2.1.8004. The user wants to
move a node that was already created to a node created after the node
being moved. After the save is performed the old node has the same
lft, rght values as before the save.

To investigate the problem, I've created a test db with only one
table. When I perform the same operation, the node is successfully
moved (i.e., the lft and rght fields are correctly updated).

I executed verify and got errors on the tree, but those same errors
exists on the test table and the function works correctly.

The model extends the AppModel and has the parentNode function as
required:

class Realm extends AppModel {

        var $name = 'Realm';
        var $actsAs = array('Tree' =>
array('left'=>'lft','right'=>'rght'),'Acl' => array('requester'));
        var $uses = array('Acl');

        var $hasMany = array(
                        'User' => array('className' => 'User',
                                                'foreignKey' => 'realm_id',
                                                'dependent' => false,
                                                'conditions' => '',
                                                'fields' => '',
                                                'order' => 'lname asc',
                                                'limit' => '',
                                                'offset' => '',
                                                'exclusive' => '',
                                                'finderQuery' => '',
                                                'counterQuery' => ''
                        )
        );
        var $belongsTo = 'Instrument';

        function parentNode() {
                if (!$this->id && empty($this->data)) {
                        return null;
                }

                $data = $this->data;
                if (empty($this->data)) {
                        $data = $this->read();
                }

                if (!$data['Realm']['parent_id']) {
                        return null;
                } else {
                        return array('Realm' => array('id' => $data['Realm']
['parent_id']));
                }
        }

The User model does not have a parentNode function.

In the realm_controller, the CRUD functions exists:
Add
                                                $params = array(
                                //'callbacks'=>false,
                                );
                        if ($this->Realm->save($this->data,$params)) {
                                //die();
                                // now need to add the permissions
                                
$this->admin_setupDefaultPerms($this->Realm->id, false);
                                // need to reload realms in app_controller so 
remove them from
                                                                //
the  session
                                // so they are reset when the page is reloaded
                                $this->Session->write('realm_ids','');
                                $this->Session->setFlash(__('The Realm has been 
saved', true));
                                $this->redirect(array('action'=>'index'));
                        } else {
                                $this->Session->setFlash(__('The Realm could 
not be saved. Please,
try again.', true));
                        }


...

Update:

                if (!empty($this->data)) {
                        $params = array(
                                'callbacks'=>false,
                                );
                //Configure::write('debug', 2);
//                        $this->Realm->useModel('Realm');
//                        $this->Realm-
>find('all',array('conditions'=>array('Realm.id'=>$id),'order'=>'lft
asc'));
                        //if ($cRealm['Realm']['parent_id'] != $this-
>data['Realm']['parent_id'])
                        //debug($this->data);
                        //die;
                        //$save_data = $this->data[0];
                        if ($this->Realm->save($this->data,$params)) {
                                $this->Session->setFlash(__('The Realm has been 
saved', true));
                                $this->redirect(array('action'=>'index'));
                        } else {
                                $this->Session->setFlash(__('The Realm could 
not be saved. Please,
try again.', true));
                        }
                } else {
                        $this->data = $this->Realm->read(null, $id);
                }

The delete and read are typical. The find uses a paginator and the
default order is Realm.id.

The SQL for table creation and insert follows:
-- phpMyAdmin SQL Dump
-- version 3.2.3
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Feb 18, 2010 at 10:58 AM
-- Server version: 5.0.88
-- PHP Version: 5.2.9

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `abcd`
--

-- --------------------------------------------------------

--
-- Table structure for table `realms`
--

CREATE TABLE IF NOT EXISTS `realms` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(75) collate utf8_unicode_ci NOT NULL,
  `description` text collate utf8_unicode_ci,
  `parent_id` int(11) default '1',
  `lft` int(11) default NULL,
  `rght` int(11) default NULL,
  `layout` varchar(75) collate utf8_unicode_ci default NULL,
  `instrument_id` int(11) NOT NULL default '2',
  `active` tinyint(4) NOT NULL default '1',
  PRIMARY KEY  (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=179 ;

--
-- Dumping data for table `realms`
--

INSERT INTO `realms` (`id`, `name`, `description`, `parent_id`, `lft`,
`rght`, `layout`, `instrument_id`, `active`) VALUES
(1, 'root', 'The root has access to everything', NULL, 1, 286, '', 2,
1),
(2, 'LVRG Corp', 'name', 1, 2, 249, '', 2, 1),
(3, 'company 1', 'test account', 2, 15, 16, '', 2, 1),
(15, 'client accounts', 'client accounts', 81, 54, 61, '', 2, 1),
(22, 'ABCD', 'ABCD trial', 2, 17, 18, '', 2, 1),
(31, 'magna', 'magna 1', 81, 62, 67, '', 2, 1),
(32, 'testclient', 'magna 5', 31, 63, 64, '', 1, 1),
(33, 'test2', 'magna 5', 31, 65, 66, '', 1, 1),
(35, 'test3', 'magna 5', 99, 86, 87, '', 1, 1),
(36, 'test4', 'magna 5', 1, 250, 251, NULL, 1, 1),
(37, 'test5', 'magna 5', 1, 252, 253, NULL, 1, 1),
(38, 'Ctest6', 'magna 5', 1, 254, 255, NULL, 1, 1),
(39, 'Ctedst7', 'magna 5', 1, 256, 257, NULL, 1, 1),
(40, 'Ctest8', 'magna 5', 1, 258, 259, NULL, 1, 1),
(41, 'compel', 'magna 5', 1, 260, 261, NULL, 1, 1),
(42, 'German', 'magna 5', 1, 262, 263, NULL, 1, 1),
(43, 'Russian', 'magna 5', 1, 264, 265, NULL, 1, 1),
(45, 'African', 'magna 5', 1, 266, 267, NULL, 1, 1),
(46, 'China', 'magna 5', 1, 268, 269, '', 1, 1),
(47, 'Japan', 'magna 5', 1, 270, 271, NULL, 1, 1),
(48, 'United States', 'magna 5', 1, 272, 273, NULL, 1, 1),
(49, 'Canada', 'magna 5', 1, 274, 275, NULL, 1, 1),
(50, 'Korea', 'magna 5', 1, 276, 277, NULL, 1, 1),
(51, 'France', 'magna 5', 1, 278, 279, NULL, 1, 1),
(53, 'Italy Films', 'magna 5', 1, 280, 281, NULL, 1, 1),
(54, 'Turkey', 'magna 5', 1, 282, 283, NULL, 1, 1),
(55, 'Botswana', 'a long comment', 2, 19, 20, '', 3, 1),
(60, 'beverly', 'highlang', 1, 284, 285, '', 2, 1),
(62, 'Portugal', 'Another land', 15, 57, 58, NULL, 2, 1),
(63, 'Brazil', '', 2, 21, 22, '', 2, 1),
(64, 'corp123', 'General', 15, 59, 60, NULL, 2, 1),
(65, 'accounting', '', NULL, 287, 288, '', 2, 1),
(66, 'Finance', 'General', NULL, 289, 290, '', 2, 1),
(67, 'Marketing', 'Country', 2, 23, 24, '', 2, 1),
(68, 'Music', 'Wazard 101', 2, 25, 26, '', 2, 1),
(69, 'Exec', 'magazine', 2, 27, 28, '', 2, 1),
(70, 'TYVM', '', 2, 29, 30, '', 2, 1),
(71, 'Norway', 'cold', 2, 31, 32, '', 2, 1),
(72, 'Iceland', 'United', 2, 33, 34, '', 2, 1),
(73, 'Mexico', 'capital', 2, 35, 36, '', 2, 1),
(74, 'Costa Rica', '', 2, 37, 38, '', 2, 1),
(75, 'Haiti', '', 2, 39, 40, '', 3, 1),
(76, 'India', 'abc', 2, 41, 42, '', 2, 1),
(77, 'Pakistan', '', 2, 43, 46, '', 2, 1),
(78, 'Israel', '', 2, 47, 48, '', 2, 1),
(79, 'Egypt', 'session', 2, 49, 50, '', 2, 1),
(80, 'Ethiopia', 'workshop', 2, 51, 52, '', 2, 1),
(81, 'Captain', '123', 2, 53, 68, '', 2, 1),
(82, 'HBC', '', 2, 69, 70, '', 2, 1),
(84, 'Dominica', '', 151, 154, 155, '', 2, 1),
(85, 'Cuba', 'Mag', 2, 71, 72, '', 2, 1),
(86, 'Dominican Republic', 'Test', 2, 73, 74, '', 2, 1),
(87, 'Peru', 'er', 2, 75, 76, '', 2, 1),
(88, 'Ecuador', 'less is more', 2, 247, 248, '', 2, 1),
(92, 'Guinea', 'Item U Course 1', 151, 156, 157, '', 2, 1),
(93, 'England', 'ABS', NULL, 291, 292, '', 2, 1),
(94, 'Ireland', 'Study', 2, 77, 78, '', 2, 1),
(95, 'Holland', 'Great', 2, 79, 80, '', 3, 1),
(97, 'Scotland', 'Rich', 2, 81, 82, '', 2, 1),
(98, 'Tinyland', 'For ', 2, 83, 84, '', 2, 1),
(99, 'leftHand', '', 2, 85, 88, '', 1, 1);

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to