我正在尝试绑定公司hasMany CompanyReview。以下是控制器中的代码:
public function review() {
$this->uses = array('Search', 'Company', 'CompanyReview');
$this->Company->bindModel(array(
'hasMany' => array(
'CompanyReview' => array(
'className' => 'CompanyReview',
'foreignKey' => 'company_id'
)
)
));
$this->CompanyReview->bindModel(
array(
'belongsTo'=>array(
'Company'=>array(
'className'=>'Company',
'foreignKey'=>'company_id'
)
)
)
);
if (!empty($this->data)) {
switch ($this->data['Search']['search_type']) {
case 'all':
$this->request->data['ReviewResults'] = $this->allReviews($this->data['Search']['search_query']);
break;
case 'company':
$this->request->data = $this->Company->find('all', array(
'conditions' => array(
'OR' => array(
'MATCH(Company.name) against ("' . $this->data['Search']['search_query'] . '" in boolean mode)',
'MATCH(CompanyReview.job_title) against ("' . $this->data['Search']['search_query'] . '" in boolean mode)'
)
)
));
break;
case 'interview':
$this->request->data['ReviewResults'] = $this->interviewReviews($this->data['Search']['search_query']);
break;
case 'salary':
$this->request->data['ReviewResults'] = $this->salaryReviews($this->data['Search']['search_query']);
break;
}
}
}
但我一直得到的错误是:数据库错误
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CompanyReview.job_title' in 'where clause'
SQL查询:
SELECT Company.id, Company.name, Company.industry_id, Company.website, Company.created FROM terbang2.companies AS Company WHERE ((MATCH(Company.name) against ("air" in boolean mode)) OR (MATCH(CompanyReview.job_title) against ("air" in boolean mode)))
注意:如果您要自定义此错误消息,请创建 app / View / Errors / pdo_error.ctp
如果您注意到,则sql不包含company_reviews表。怎么回事?请帮忙!。感谢
哦,顺便说一句,在company_reviews表中有一个名为job_title的表行。
答案 0 :(得分:0)
您需要使用[Joins]。
从关联自动构建的“正常”查询实际上为每个模型分别进行查询,因此您无法引用其表中不存在的字段,并且会导致错误(如您所说的那样)如果试过的话。
答案 1 :(得分:0)
如果你想让蛋糕自动化使用$this->CompanyReview->find();
,因为只有在$belongsTo
和$hasOne
的情况下,蛋糕才会加入。在$hasMany
和HABTM中有单独的查询。如果你想/需要在公司找到,你需要做@Dave提到的连接。
请注意。 您不需要双向绑定。如果您确实在公司上找到,则仅绑定到公司模型。