0

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 FROM cakead.jobs AS Job LEFT JOIN cakead.qualifications AS Qualification ON (Job.qualification_id = Qualification.id) WHERE JobCategory = ('1')

1 Answer 1

0

try this

//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);
          }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.