0

我正在尝试绑定 Company 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 的表行。

4

2 回答 2

0

您需要使用[Joins]

从关联自动构建的“正常”查询实际上对每个模型执行单独的查询,因此您不能引用其表中不存在的字段,如果试过了。

于 2012-12-29T04:55:25.023 回答
0

如果你想让 cake automagic 工作使用$this->CompanyReview->find();,因为 cake 只在 and 的情况下才$belongsTo加入$hasOne。在$hasMany和 HABTM 中有单独的查询。如果您想/需要在公司上查找,您需要进行@Dave 提到的加入。

笔记。您不需要双向绑定。如果您确实在 Company 上找到,则仅绑定到 Company 模型。

于 2012-12-29T13:38:31.940 回答