我有三个表格jobs
,categories
和job_categories
与以下结构相似:
**jobs**
id title desc
**categories**
id name
**job_categories**
id job_id category_id
所以我想检索属于特定类别的所有作业。为此,我在view()
CategoriesController.php
function view($category_id = NULL)
{
if($category_id != NULL)
{
$conditions['JobCategory']['category_id'] = $category_id;
$all_jobs = $this->Job->find('all',array('conditions' => $conditions));
$this->set('all_jobs',$all_jobs);
}
}
并添加了$hasMany
到Category.php
模型:
class Category extends AppModel {
var $name = 'Category';
var $hasMany = array('JobCategory');
}
但是当我查看视图时,它显示错误:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'JobCategory' in 'where clause'
及其生成的查询:
选择
Job
。id
,Job
。job_title
,Job
。job_description
,Job
。job_skills
,Job
。contact_number
,Job
。contact_email
,Job
。qualification_id
,Job
。experience
,Job
。categories
,Job
。remarks
,Job
。support_image
,Job
。freshers_apply
,Job
。added_on
,Job
。status
,Qualification
。id
,Qualification
。name
FROMcakead
。jobs
ASJob
LEFT JOINcakead
。qualifications
ASQualification
ON (Job
。qualification_id
=Qualification
。id
)WHERE JobCategory = ( '1')
答案 0 :(得分:0)
试试这个
// AppModel.php
public $actAs = array('Containable');
// Category.php
class Category extends AppModel {
var $name = 'Category';
var $hasAndBelongsTo = array('Category');
}
// CategoriesController.php
function view($category_id = NULL)
{
if($category_id != NULL)
{
$conditions['Category']['id'] = $category_id;
$all_jobs = $this->Category->find('all',array('conditions' => $conditions));
$this->set('all_jobs',$all_jobs);
}
}