簡體   English   中英

Yii-如何從另一個模型獲取數據

[英]Yii - How to get data from another model

早上好

如何從其他模型獲取數據? 我有一個搜索字段,需要在其中搜索項目名稱。 然后我的Cgridview將顯示選定的項目。

我的關系中有這個

public function relations() {
        return array(
            'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),
        );
    }

我試圖在模型的搜索功能中訪問project_name。

public function search($employee, $search_date_start, $search_date_end, $search) {

        $criteria = new CDbCriteria;
        $criteria->with = array('eval' => array('together' => true));

        $criteria->compare('employee_id', $this->employee_id);
        $criteria->compare('remarks', $this->remarks, true);
        $criteria->compare('eval_id', $this->eval_id);

        //I tried it like this
        $criteria->addSearchCondition('project.project_name', $search);           

        if ($employee != '') {
            $criteria->compare('t.employee_id', $employee->company_id);
        }    
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

當我這樣做時,我得到一個錯誤。

CDbCommand無法執行SQL語句:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ project.project_name”。 執行的SQL語句是:SELECT COUNT(DISTINCT tid )FROM trx_evaluation_details t LEFT OUTER JOIN trx_evaluation eval ON( teval_id = evalid )WHERE((project.project_name LIKE:ycp0)

我的代碼有什么問題?? 我嘗試在當前模型中連接RmProject模型,以便可以訪問project_name ..但是,出現此錯誤。 請幫忙..

這是一個編輯:

這是我整個關系的一部分

 public function relations() {
        return array(
            'eval' => array(self::BELONGS_TO, 'Evaluation', 'eval_id'),
            'project' => array(self::BELONGS_TO, 'RmProjects', 'project_id'),
        );
    }

我已在模型中添加了此功能,但仍然無法正常工作。 它只是改變了桌子。

$criteria->with = array('project' => array('together' => true));     
$criteria->addSearchCondition('project.project_name', $search);

這是我的搜索功能。

public function search($employee, $search_date_start, $search_date_end, $search) {            
        $criteria = new CDbCriteria;
        $criteria->with = array('eval' => array('together' => true));
        $criteria->with = array('project' => array('together' => true));                       

        $criteria->compare('employee_id', $this->employee_id);
        $criteria->compare('remarks', $this->remarks, true);
        $criteria->compare('eval_id', $this->eval_id);            

        $criteria->addSearchCondition('project.project_name', $search);
        $criteria->addSearchCondition('start_date', $search_date_start, 'AND');
        $criteria->addSearchCondition('end_date', $search_date_end, 'AND');

        if ($employee != '') {
            $criteria->compare('t.employee_id', $employee->company_id);
        }    

         if ($search_date_end !== '' && $search_date_start !== '' && $search !== '') {
                $criteria->condition = "start_date  >= '$search_date_start' AND end_date <= '$search_date_end' AND project.project_name like '%$search%'AND t.employee_id = '$employee->company_id'";                
            }

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

在此處輸入圖片說明

您沒有在搜索方法中添加模型

$criteria->together = true;
$criteria->with = array('eval','project');
$criteria->addSearchCondition('project.project_name', $search);

同樣,通過方法的屬性設置搜索值也不是一個好的解決方案。 定義新的模型公共屬性,定義規則並在搜索中使用它。

public $project_name;

public function attributeLabels(){
   return array(
       //... your other labels there
       'project_name' => 'Project Name',
   );
}

public function rules(){
   return array(
      //... your other rules there
      array('project_name', 'safe', 'on' => 'search'),
   );
}

public function search(){
   $criteria->compare('project.project_name', $this->project_name);
}

我已經解決了這個問題。

在我的Relations()中,我添加了這一行

'project' => array(self::HAS_ONE, 'RmProjects', array ('project_id'=>'project_id'), 'through'=> 'eval'),

這將加入具有通過另一個模型的連接的模型。 然后您可以從另一個模型中獲取數據。.我希望這個答案可以對與我有相同問題的任何人有所幫助。

您可以在此處閱讀它。. 通過 http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through 使用關系查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM