Get data from another Model

Asked

Viewed 391 times

1

I’m not getting a phone related to a user on View edit(). I can get the phone number on ver() but not in the 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 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 View Errors fatal_error.ctp app

Thank you in advance.

1 answer

2


Straight from a Controller, you only have access to your Model correspondent. To gain access to Model related, which in this case is the Phone, you need to do so:

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

That’s what the mistake’s about "Call to a Member Function find() on null" refers.

  • Well it listed now, but when I modify the phone it does not update in the database.

  • That’s another question: the method to save is saveAll(), with the To uppercase. PHP is case-sensitive.

Browser other questions tagged

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