1

Não estou conseguindo obter um telefone relacionado a um usuário na View edit(). Consigo obter o número do telefone no ver() mas não no 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');
}

Quando eu tento acessar a página dá o seguinte erro:

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

Agradeço desde já.

1 Resposta 1

2

Diretamente de um Controller, você só tem acesso ao seu Model correspondente. Para obter acesso ao Model relacionado, que no caso é o Phone, você precisa fazer assim:

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

É sobre isso que o erro "Call to a member function find() on null" se refere.

2
  • Bom ele listou agora, mas quando eu modifico o telefone ele não atualiza no banco de dados. 16/04/2015 às 19:19
  • Isso já é outra questão: o método para salvar é saveAll(), com o A maiúsculo. O PHP é case-sensitive. 16/04/2015 às 19:32

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .