Frage

I'm having an issue with initiating the Tree behavior issue in CakePHP. I have set up the table with lft, rght and parent_id columns.

I'm adding a first entry which other can refer to with the parent_id column. However, the referring does not work as I want.

The first entry is added to the database as expected. If I don't specify parent_id that column will get NULL as database value. The lft and rght columns is also getting NULL as value. I'm not sure if that's what it's supposed to do.

When I'm trying to add a new entry after the initial one, the save will fail.

I've tried to set the lft and rght columns to 1, 1 and 1, 2 on the first entry, but without any luck.

Am I doing something wrong here?

War es hilfreich?

Lösung

You definitely do not want to set lft and rght. Those are handled by the framework. Do you have them defaulting to null as the docs specify?

I have always set the parent_id when using Tree Behavior and then CakePHP automagically handles the tree structure based on the parent id. if you need to move the item within the branch, then you can use the other functions of the behavior to do that.

Try setting just the parent_id and then save. Let me know if that doesn't work.

Andere Tipps

It's interesting but this is an undocumented part of Cake php. The docuamntation says: top element of the tree needed to set parent_id to "null". But when you save the elemet on edit, the reorder not works because there is an interesting condition when reorder looking for the children:

    if ($id === null && $Model->id) {
        $id = $Model->id;
    } elseif (!$id) {
        $id = null;
    }

Around the TreeBehavior line 300.

So if you call reorder with id=>null and you have an $Model->id the actual tree item will be reordered only. It's wrong, i would like to reorder whole of my tree because i have changed my order field on a top category item! I think it's a kind of bug.

You have to set the id=>'' as empty, then the whole tree will be reordered. That's you want. When you editing your top tree item and would like to reorder you have to call the reorder with id=>''

        if(!empty($this->data['WebshopCategory']['parent_id'])){
            $this->reorder ( array('id' => $this->data['WebshopCategory']['parent_id'], 'field' => 'ordered', 'order' => 'ASC', 'verify' => true) );
        }else{
            $this->reorder ( array('id'=>'', 'field' => 'ordered', 'order' => 'ASC', 'verify' => true) );
        }

It will reorder whole of your tree. Note: with a lot of tree items the save will slow, so you have to check your order field was changed or not.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top