我正在尝试使用配置文件和资格表之间的HABTM关联。
型号: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'
)
);
}
型号: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 */
}
}
作为评论提到的所有三次尝试都给我一个错误说:
Error: Call to a member function find() on a non-object
File: ~/app/Controller/ProfilesController.php
Line: xxx
我想知道如何生成资格表中所有条目的列表? 而且,我的代码现在的错误是什么?
答案 0 :(得分:0)
在您的个人资料模型中,HABTM与资格认证关系的别名是“资格”,因此您必须在控制器内使用:$ qualifications = $ this-> Profile-> Qualifications-> find('list “);或从模型中删除复数。
为了防止出现这类错误并节省您的时间,使用cake bake console或online CakePHP baking tool自动生成代码非常有用。