Change Children id to another id

Asked

Viewed 39 times

0

I made a panel with some functions of Tree, edit, move and delete. Until then my erasing is working when I want to erase a specific thing without children. The problem is that when I delete an item that has children, it erases all the children together, and my intention was that it would change the parent_id of the children from the item that will be deleted, to the item that I selected in the list.

PS: that $this->request->data['Navigation']['childs'] is the id I have a menu with a list returning all the ids I have available so that the person can change them.

public function apagar($id){
        if ($this->request->is('post')){
            empty($this->request->data['Navigation']['childs']) ? null : $this->request->data['Navigation']['childs'];
            $this->Navigation->updateAll(array(
            'Navigation.parent_id' => $id,
             ), array(
            'Navigation.parent_id' => $this->request->data['Navigation']['childs'],
            ));
            $this->Navigation->id = $id;
            $this->Navigation->delete();
            $this->Session->setFlash('Menu Apagado!');
            return $this->redirect('/navegacao');
        } else {
            $tree = $this->Navigation->generateTreeList(null, null, null, '   - ');
            unset($tree[$id]);
            $this->set('tree', $tree);
        }
    }

1 answer

0

Note that this line below does not result in anything.

empty($this->request->data['Navigation']['childs']) ? null : $this->request->data['Navigation']['childs'];

To be honest, I don’t quite understand what your logic is about deleting the record or not. But apparently, his intention was to use the value of his column childs to make this update. So you need to do something more or less like this:

if (!empty($this->request->data['Navigation']['childs'])) {
    // Possui filhos
    $this->Navigation->updateAll(
                array('Navigation.parent_id' => $id),
                array('Navigation.parent_id' => $this->request->data['Navigation']['childs'])
    );
} else {
    // Não possui filhos
}
  • It changes the parent_id, but it doesn’t delete the id the guy clicks, there are the changed ids, but the id I clicked to delete is still there.

  • Try switching to $this->Navigation->delete($id).

  • I put this, but then, it excludes the id, and his son, IE, ends up excluding everything.

  • If you are updating relationship ids before deleting, you have no reason to delete these children as well. In his Model, where this relationship was declared, the property dependent is defined as false?

  • My model is just like this:public $useTable = 'navigation';
 
 public $displayField = 'nome';
 
 public $actsAs = array('Tree'); I really don’t understand why when I delete id, it deletes all parents_id that is related to it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.