2012-01-27 38 views
3

的Yii 1.1應用程序的開發食譜解釋利用相關活動記錄模型的數據用於搜索相關模型和方法。該方法在第193和194頁進行了解釋。我試圖將此方法集成到我的應用程序中,但它不起作用。可能有人解釋我這個功能是不是還在Yii框架版本1.1.8Yii框架:採用關聯的Active Record模型的數據進行搜索

在此位置也是我能找到的搜索有關的活動記錄模型的數據形式的評論。但它也行不通。 http://www.yiiframework.com/doc/api/1.1/CDbCriteria

我有順序表和用戶表

Order表和用戶表有一對多的關係。

用戶有很多訂單和訂單隻有一個用戶。

所以,我編輯以下CDbCriterial包括用戶表的名字和電子郵件領域的訂購表中搜索條目。

Order表有如下關係

public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the relations automatically generated below. 
    return array(
     'comments' => array(self::HAS_MANY, 'Comment', 'order_id'), 
     'user' => array(self::BELONGS_TO, 'User', 'user_id'), 
     'orderstatus' => array(self::BELONGS_TO, 'Orderstatus', 'orderstatus_id'), 
     'commentCount' => array(self::STAT, 'Comment' , 'order_id') 
    ); 
} 

這是申請列入

public function search() 
    { 
     // Warning: Please modify the following code to remove attributes that 
     // should not be searched. 

     $criteria=new CDbCriteria; 

     $criteria->compare('id',$this->id); 
     $criteria->compare('order_create_date',$this->order_create_date,true); 
     $criteria->compare('price',$this->price,true); 
     $criteria->compare('bank_account_number',$this->bank_account_number,true); 
     $criteria->compare('hardwaredetail_Id',$this->hardwaredetail_Id); 
     $criteria->compare('user_id',$this->user_id); 
     $criteria->compare('order_update_date',$this->order_update_date,true); 
     $criteria->compare('is_received',$this->is_received); 
     $criteria->compare('order_received_date',$this->order_received_date,true); 
     $criteria->compare('is_notify_by_email',$this->is_notify_by_email); 
     $criteria->compare('warehouse_offered_price',$this->warehouse_offered_price,true); 
     $criteria->compare('warehouse_offered_price_date',$this->warehouse_offered_price_date,true); 
     $criteria->compare('orderstatus_id',$this->orderstatus_id); 
     $criteria->together = true; 
     $criteria->with = array('user'); 
     $criteria->compare('user.name',$this->user,true); 
     //$criteria->compare('user.name',$this->user->name); 
     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 

和訂單管理編輯頁面顯示提交的名稱如下

搜索/篩選條件與用戶表的名稱
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'order-grid', 
    'dataProvider'=>$model->search(), 
    //'filter'=>$model, 
    'columns'=>array(
     'id', 
     'order_create_date', 
     'price', 
     'bank_account_number', 
     array(
      'name'=>'user', 
      'value'=>'$data->user->name' 
     ), 
     ), 
)); 

返回的錯誤消息

enter image description here

通過施加thaddeusmt給我已經面臨以下錯誤消息的溶液解決id列模糊問題之後。

enter image description here

預先感謝任何幫助

+0

你可以發佈一些你的代碼嗎?至少是樣品.. – 2012-01-27 09:59:07

+0

我已經按照您的要求添加了我的代碼 – KItis 2012-01-27 10:34:17

+0

您收到任何錯誤?並添加了「public $ user」;在第一線順序排列模型? – user677900 2012-01-27 13:11:02

回答

11

我可以找到答案。這就是我所做的一切。

以下是我擁有的兩張桌子。爲了和用戶

enter image description here

我有訂單/管理頁面,默認生成的代碼提供僅搜索順序表中的數據。我想在搜索條件中關聯用戶表名字段。

這是搜索頁面的初始外觀。

enter image description here

我想從其他表格提交這個搜索結果集成的用戶名。那麼最終的樣子如下。

enter image description here

所以這些都是我做的步驟。

首先在訂貨型號我有以下關係

public function relations() 
     { 
      // NOTE: you may need to adjust the relation name and the related 
      // class name for the relations automatically generated below. 
      return array(

       'user' => array(self::BELONGS_TO, 'User', 'user_id'), 


      ); 
     } 
This is generated by Yii framework , i did nothing :) 

Then , i changed the search method as follows 

public function search() 
    { 
     // Warning: Please modify the following code to remove attributes that 
     // should not be searched. 

     $criteria=new CDbCriteria; 


     $criteria->compare('t.order_create_date',$this->order_create_date,true); 
     $criteria->compare('t.price',$this->price,true); 
     $criteria->compare('t.bank_account_number',$this->bank_account_number,true); 
     $criteria->compare('t.hardwaredetail_Id',$this->hardwaredetail_Id); 
     //$criteria->compare('user_id',$this->user_id); 

     $criteria->compare('t.order_update_date',$this->order_update_date,true); 
     $criteria->compare('t.is_received',$this->is_received); 
     $criteria->compare('t.order_received_date',$this->order_received_date,true); 
     $criteria->compare('t.is_notify_by_email',$this->is_notify_by_email); 
     $criteria->compare('t.warehouse_offered_price',$this->warehouse_offered_price,true); 
     $criteria->compare('t.warehouse_offered_price_date',$this->warehouse_offered_price_date,true); 
     $criteria->compare('t.orderstatus_id',$this->orderstatus_id); 
     $criteria->together = true; 
     $criteria->compare('t.id',$this->id,true); 
     $criteria->with = array('user'); 
     $criteria->compare('name',$this->user,true,"OR"); 
     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 

把噸,前面的t如果您的訂單表的主鍵字段如果兩者都具有節省的名字是很重要的。在我的情況下,它是ID和ID,所以我不得不把t。

其它的事情是元素

$ criterial-> togeter =真的順序;應該在關係元素之前。

然後u更新爲Order表中的rules方法。我添加了用戶提交和名稱歸檔到安全屬性。

public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      //array(' orderstatus_id', 'required'), 
      array('hardwaredetail_Id, user_id, is_received, is_notify_by_email, orderstatus_id', 'numerical', 'integerOnly'=>true), 
      array('price, warehouse_offered_price', 'length', 'max'=>10), 
      array('bank_account_number', 'length', 'max'=>100), 
      array('order_create_date, order_update_date, order_received_date, warehouse_offered_price_date, user,name', 'safe'), 
      // The following rule is used by search(). 
      // Please remove those attributes that should not be searched. 
      array('id, order_create_date, price, bank_account_number, hardwaredetail_Id, user_id, order_update_date, is_received, order_received_date, is_notify_by_email, warehouse_offered_price, warehouse_offered_price_date, orderstatus_id', 'safe', 'on'=>'search'), 
     ); 
    } 

最後更新您的UI代碼。

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'order-grid', 
    'dataProvider'=>$model->search(), 
    'filter'=>$model, 
    'columns'=>array(
     'id', 
     'order_create_date', 
     'price', 
     'bank_account_number', 
     array(
      'name'=>'user', 
      'value'=>'$data->user->name' 
     ) 
)); ?> 

我更新了訂單管理與

array(
       'name'=>'user', 
       'value'=>'$data->user->name' 
      ) 

這是我做的,它爲我工作。問我是否需要幫助。感謝每一位尋找這個問題的人。

+1

我站在糾正,我不認爲你可以使用'用戶'作爲列名 - 我不認爲'$ model-> attributes'會爲'search()'方法設置數據。每天學些新東西! – thaddeusmt 2012-01-27 15:39:47

+0

添加用戶提交的和名稱歸檔到安全屬性可以被添加到安全搜索規則。 – 2014-03-15 18:20:05

+0

快速搜索(ajax)不起作用。奇怪而真實的高級搜索作品。爲什麼? – jasmines 2017-05-16 11:14:49

2

它看起來像兩個userorder已列名爲id。並且您的標準在WHERE子句中使用它們,這會給出「模糊」的mySql錯誤消息。

當使用with標準(它一個SQL JOIN表的),如果你的兩個表中具有相同名稱的列,你需要使用MySQL的「點」字頭的情況下,像這樣:

$criteria->compare('t.id',$this->id); // the default table prefix in Yii is "t" 

當我加入一個使用$criteria->with的表格時,我加了所有列名稱的前綴(在我的comparecondition條件等)。,像這樣:

$criteria->compare('t.id',$this->id); // t 
$criteria->compare('t.order_create_date',$this->order_create_date,true); // t 
$criteria->with = array('user'); 
$criteria->compare('user.name',$this->user_id,true); // user (the filter will set user_id) 

你的GridView將需要看起來像這樣:

array(
    'name'=>'user_id', 
    'header'=>'User', 
    'sortable'=>false, 
    'value'=>'$data->user->name' 
), 

另外,我覺得還有一個更大的問題,正如你指出你的編輯:

你搜索功能設置如下:user.name',$this->user - 但$this->user將通過關係返回當前用戶對象,而不是搜索條件。列過濾器將設置user_id屬性。

編輯:不,你可以$this->user作爲列名,只要你將它設置爲safe屬性。

我解決這個越來越方式顯示在這裏更詳細:

Search in BELONGS_TO model column with CGridView, Yii

排序不會與工作,雖然 - 只是過濾。

這裏是在Yii的論壇,可能會給你更多的線索好的帖子,太:

http://www.yiiframework.com/forum/index.php?/topic/9083-search-filter-of-a-relations-field-through-cgridview/

可悲的是,排序和過濾關係是CGridViews不是真的默認功能。 YOu可以很容易地顯示相關信息,其列名稱如user.name,但不會排序或篩選。祝你好運!

+0

後應用你提到的解決方案的問題,我得到新的錯誤。我用新的錯誤信息更新了這個問題。 – KItis 2012-01-27 15:01:54

+0

謝謝thaddessmt的答案,我可以解決這個問題。你的解決方案幫助很大 – KItis 2012-01-27 15:15:04

+0

我更新了一些更多的解釋,我希望它有幫助。 – thaddeusmt 2012-01-27 15:36:29

相關問題