Cakephp有很多没有JOINing表

时间:2014-03-25 02:42:10

标签: sql cakephp has-many

我有三个表格jobscategoriesjob_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);
          }
    }

并添加了$hasManyCategory.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'及其生成的查询:

  

选择JobidJobjob_titleJobjob_description,   Jobjob_skillsJobcontact_numberJobcontact_email,   Jobqualification_idJobexperienceJobcategories,   JobremarksJobsupport_imageJobfreshers_apply,   Jobadded_onJobstatusQualificationid,   Qualificationname FROM cakeadjobs AS Job LEFT JOIN   cakeadqualifications AS Qualification ON   (Jobqualification_id = Qualificationid)WHERE JobCategory =   ( '1')

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