0

I am trying to work with HABTM association between Profiles and Qualifications tables.

Model: Profile.php

App::uses('AppModel', 'Model');
class Profile extends AppModel {
    public $hasAndBelongsToMany = array(
        'Qualifications' => array(
        'className' => 'Qualification',
        'joinTable' => 'profile_qualifications',
        'foreignKey' => 'profile_id',
        'associationForeignKey' => 'qualification_id',
        'unique' => 'keepExisting'
        )
    );
}

Model: Qualification.php

App::uses('AppModel', 'Model');
class Qualification extends AppModel {
    public $hasAndBelongsToMany = array(
        'Profile' => array(
        'className' => 'Profile',
        'joinTable' => 'profile_qualifications',
        'foreignKey' => 'qualification_id',
        'associationForeignKey' => 'profile_id',
        'unique' => 'keepExisting',
        )
    );
}

Controller: ProfilesController.php

App::uses('AppController', 'Controller');
class ProfilesController extends AppController {
    public function add() {
        $qualifications = $this->Qualification->find('list'); /* Attempt 1 */
        $qualifications = $this->Profile->Qualification->find('list'); /* Attempt 2 */
        $qualifications = $this->Profile->ProfileQualification->Qualification->find('list'); /* Attempt 3 */
    }
}

All three attempts mentioned as comment have given me an error saying:

Error: Call to a member function find() on a non-object
File:  ~/app/Controller/ProfilesController.php
Line:  xxx

I want to know how can I generate a list of all entries in Qualifications table ? Moreover, what is the mistake in my code right now ?

i01000001
  • 119
  • 1
  • 2
  • 9

1 Answers1

0

In your Profile Model, the alias of your HABTM relation with Qualification is "Qualifications", so inside the controller you have to use : $qualifications = $this->Profile->Qualifications->find('list'); or remove the plural from the model.

To prevent these kind of mistakes and save your time, it's really useful to use automatic code generation with the cake bake console or an online CakePHP baking tool.

Chris
  • 429
  • 2
  • 8
  • Thanks a lot mate for pointing that out! :) I did use cake bake console to create this model and then tried showing my smartness which turned really bad :( – i01000001 May 01 '13 at 15:34