creocoder / yii2-nested-sets

The nested sets behavior for the Yii framework.
Other
446 stars 129 forks source link

How to temporarily detach this behaviour? #58

Closed gb5256 closed 9 years ago

gb5256 commented 9 years ago

Hello, I need to temporarliy disable the behaviour. In yii1 I used this on the model where the behaviour is attached to:

$model->detachBehavior('nestedSetBehavior');

But this is not working in the updated version. Or is this a yii2 issue?

Thanks for any hints... (again) gb5256

creocoder commented 9 years ago

Ensure you have behavior configuration like follows:

    public function behaviors() {
        return [
            'tree' => [
                'class' => NestedSetsBehavior::className(),
            ],
        ];
    }

Now you can detach it by code:

$node->detachBehavior('tree');
gb5256 commented 9 years ago

Thanks for your quick answer. I tried it but my server freezes when I do this. What I try to achieve is that a form is allowing batch importing. For this I need to deactivate the bahviour. I try this:

public function import() {
    $model = new Node();

   if ($model->load(Yii::$app->request->post())) {

        $model->detachBehavior('tree');
        if ($model->save()) {
            return $this->redirect(['/map/view', 'id' => $map_id]);
        }
       } else {
        $model->map_id = $map_id;
        return $this->renderPartial('_form', [
                    'model' => $model,
        ]);
       }
   }

I can see in the database that the new records are created, so the $model->save() is working. Hmmm. Do I need to attach the behaviour again? I thought that the redirect should reset this as I am calling a redircet to a complete different model...

creocoder commented 9 years ago

I do not see any batch import code here.

gb5256 commented 9 years ago

Oh, sorry. The batch import comes later. I right now just try to create a new node. Just one new record. But I think I have an error somewhere else, let me check something. Thanks for now... again....

gb5256 commented 9 years ago

ok, found the problem. There was a beforeSave which was making problems. So to close this issue: $node->detachBehavior('tree'); is working as expected.

Thanks.