Question

I want to be able to delete a User, but a User has a Manager:

var $belongsTo = array(
        'Manager' => array(
            'className' => 'User',
            'foreignKey' => 'manager_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );

And whenever a User is deleted, all of it's "children" are deleted too.

For example, say I delete User A. User A is the manager of users B, C, and D. When A is deleted, so are B, C, and D, because they have A as their manager_id.

So my question is - is this supposed to be happening? And is there a way I can prevent this from happening?

Thanks!

Was it helpful?

Solution 2

It was an ACL issue - totally unexpected. Since our users operate in a tree structure with the Manager, the User has a lft and rght field that is only updated in the afterSave. The easy solution is to dissociate the user by setting their lft and rght to 0, but after deeper thought, I am setting their manager_id to NULL and saving it so that the tree reorganizes itself (via the afterSave).

Wow. That was quite the problem.

OTHER TIPS

Read the book, that is intended behavior and you can stop it by adding

'dependent' => false,

to the associations configuration array.

See http://book.cakephp.org/1.2/en/view/78/Associations-Linking-Models-Together and search for "dependent" on this page.

And I recommend you to use 2.0 if it's a new project, 1.2 is deprecated for a long time now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top