I'm using the TreeBehaviour on a Page model and I noticed that when
records are ADDED with no parent selected they are saved in the
database with a parent_id of 0, but when records are EDITED and given
no parent they saved with NULL as the parent_id.

My add and edit controller logic is almost identical:
        function admin_add() {

                if (empty($this->data)) {
                        $this->set('pages',$this->Page-
>generatetreelist(null,'{n}.Page.id','{n}.Page.name',' - '));
                }
                else {
                        if($this->Page->save($this->data)) {
                                $this->redirect(array('action' => 'index'), 
null, true);
                        }
                }
        }



        function admin_edit($id) {


                if (empty($this->data)) {
                        $this->Page->id = $id;
                        $this->data = $this->Page->read();
                        $this->set('pages',$this->Page->generatetreelist('id 
!='.
$id,'{n}.Page.id','{n}.Page.name',' - ')); // ensures itself isn't an
option as a parent
                }
                else {
                        $this->Page->save($this->data);
                        $this->redirect(array('action' => 'index'), null, true);
                }
        }

---------------------
here's the add view:

        <?php echo $form->create('Page') ?>

        <?php echo $form->input('name') ?>

        <?php echo $form->input('body_html') ?>

        <?php echo $form->input('parent_id',array('type'=>'select','options'=>
$pages,'empty' => true)) ?>

        <?php echo $form->input('active') ?>

        <?php echo $form->end('Save') ?>

---------------------
and the edit view:

        <?php echo $form->create('Page') ?>

        <?php echo $form->hidden('id')?>

        <?php echo $form->input('name') ?>

        <?php echo $form->input('body_html') ?>

        <?php echo $form->input('parent_id',array('type'=>'select','options'=>
$pages, 'selected'=>$this->data['Page']['parent_id'],'empty'=>true )) ?
>

        <?php echo $form->input('active') ?>

        <?php echo $form->end('Save')?>

This causes a problem where the order changes if a recorded is edited,
as the parent changes from 0 to NULL.

Any ideas how I can avoid this and ensure all new records without a
parent are given NULL instead of 0 as their parent_id?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to