sergserg
sergserg

Reputation: 22264

Call to a member function create() on a non-object

I'm trying to create a new record on a table called tutors. I named it that way in order to follow CakePHP conventions.

$codigoAlumnoNuevo = $this->Persona->id;
$tipoPersona = $this->request->data('TipoPersona'); # Just grabbing a POSTED value.

switch ($tipoPersona) {
    case '0': # Es Tutor
        $this->Tutor->create();
        $this->Tutor->PersonaId = $codigoAlumnoNuevo;
        $this->Tutor->save();
        die("Saved record");
        break;

    case '1': # Es Alumno
        $this->Tutor->create();
        $this->Tutor->PersonaId = $codigoAlumnoNuevo;
        $this->Tutor->save();
        die("Saved record");
    break;

    case '2': # Es Coordinador de Programa
        $this->Tutor->create();
        $this->Tutor->PersonaId = $codigoAlumnoNuevo;
        $this->Tutor->save();
        die("Saved record");
    break;

    default:
        break;
}

When trying to run this code, the following exception fires:

Call to a member function create() on a non-object

If I try this, I can see that $this->Tutor is NULL:

var_dump($this->Tutor);

But in the same controller, I can call $this->Persona->create(); just fine; $this->Persona is not null. Why is this? Why is Persona available but not Tutor?

Is it a naming thing?

Here's my database model:

enter image description here

Upvotes: 0

Views: 822

Answers (2)

dhofstet
dhofstet

Reputation: 9964

If you defined a hasMany relationship between Persona and Tutor in your Persona model, you can access the Tutor model with $this->Persona->Tutor->create() and the $uses array from Joseph's answer is not necessary.

Upvotes: 1

Joseph
Joseph

Reputation: 2712

From your PersonasController you can only access the Persona model by default.

If you want to run creates on the Tutor model, you need to add var $uses = array('Persona','Tutor'); to the beginning of your PersonasController.php like so:

<?php
class PersonasController extends AppController {
    var $uses = array('Persona','Tutor');

    ....functions.....

}
?>

Upvotes: 1

Related Questions