0

When I use the Tree behavior and scaffold my controller, my parent_id shows up in the select box on the add view; however, when I bake my controller and views, I end up with an empty select box for parent_id. I know it is something simple, but I just cannot figure it out.

Here is my Controller:

public function add() {
    if ($this->request->is('post')) {
        $this->Test->create();
        if ($this->Test->save($this->request->data)) {
            $this->Session->setFlash(__('The test has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The test could not be saved. Please, try again.'));
        }
    }
    $parentTests = $this->Test->ParentTest->find('list');
    $this->set(compact('parentTests'));
}

And here is my model:

public $belongsTo = array(
    'ParentTest' => array(
        'className' => 'Test',
        'foreignKey' => 'parent_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

public $hasMany = array(
    'ChildTest' => array(
        'className' => 'Test',
        'foreignKey' => 'parent_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

If anyone can help me figure this out, I would greatly appreciate it. Thank you in advance.

1 Answers1

0

Bake might not resolve it properly. You have to adjust it manually, rename the variable to parents:

$parents = $this->Test->ParentTest->find('list');
$this->set(compact('parents'));
mark
  • 21,691
  • 3
  • 49
  • 71