Cakephp belongs to relation - access to related model

I have a consultants table with a "specialty_id" foreign key that is associated with the "specilaties" table.

class Consultant extends AppModel {
    public $belongsTo = array(
        'Specialty' => array(
            'className'     => 'Specialty',
            'conditions'    => array('Specialty.active' => 1)
        )
    );
}

class Specialty extends AppModel {
    public $hasOne = 'Consultant';
}

      

I think this is correct, however I cannot get the list of specialties from the consultant controller ("Calling member function () for non-object")

$this->set('specialties', $this->Specialty->find('all'));

      

Where am I going wrong?

thank

+3


source to share


2 answers


Remember you are in the controller, not the model. Try the following:



$this->set('specialties', $this->Consultant->Specialty->find('all'));

      

+2


source


If you are using a model in another controller, first load that model and then run the query:



$this->loadModel('Specialty');

      

+1


source







All Articles