Get data from another Model

1

I'm not getting a phone related to a user in View edit() . I can get the phone number in ver() but not edit() .

Controller Patient :

public function edit($id = null) {
    if (!$this->Patient->exists($id)) {
        throw new NotFoundException(__('Invalid patient'));
    }

    if ($this->request->is(array('post', 'put'))) {
        if ($this->Patient->saveall($this->request->data)) {
            $this->Session->setFlash(__('The patient has been saved.'));
            return $this->redirect(array('action' => '../patients/index'));
        } else {
            $this->Session->setFlash(__('The patient could not be saved. Please, try again.'));
        }
    } else {
        $phone = $this->Phone->find('all');
        $this->set(array('phone' => $phone));
        $options = array('conditions' => array('Patient.' . $this->Patient->primaryKey => $id));
        $this->request->data = $this->Patient->find('first', $options);
    }
}

View:

<?php foreach ($phone as $phones): ?>  

<?php
    echo $this->Form->input('phone_number',array('label'=>'Telefone','value'=>$phones['Phone']['phone_number']),array('class'=>'form-control'));
?>

<?php endforeach; ?>    

Model Patient :

public $hasMany = array('Phones'=> array('dependent' => true)) ;
}

Model Phone :

public $belongsTo = array('Patient');
}

When I try to access the page it gives the following error:

  

Error: Call to a member function find () on null File:   C: \ xampp \ htdocs \ cifisio \ app \ Controller \ PatientsController.php Line: 87

     

Notice: If you want to customize this error message, create   app \ View \ Errors \ fatal_error.ctp

Thank you in advance.

    
asked by anonymous 16.04.2015 / 20:14

1 answer

2

Directly from Controller , you only have access to your corresponding Model . To get access to Model related, which in this case is Phone , you need to do this:

$phone = $this->Patient->Phone->find('all');

This is what the error "Call to a member function find () on null" refers to.

    
16.04.2015 / 20:28