I have three tables jobs
,categories
and job_categories
with similar to below structure:
**jobs**
id title desc
**categories**
id name
**job_categories**
id job_id category_id
So I want to retrieve all the jobs belongs to a particular category. For that I wrote an action view()
in 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);
}
}
And added a $hasMany
to Category.php
model:
class Category extends AppModel {
var $name = 'Category';
var $hasMany = array('JobCategory');
}
But when I checked the view , it shows error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'JobCategory' in 'where clause'
and the query it generates :
SELECT
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')