2013-10-15 23 views
0

我有一個名爲test.php的模型類文件。我有兩個字段 - Function typeService Type。所以,現在我想看看Function typeService Type都是唯一的。即Function typeService Type在多行中不能相同。如何查看字段值在表中是否唯一

我正在使用創建和更新功能。我希望這兩個函數看看是否重複Function typeService Type,然後不保存它,如果不重複,則保存它。

我用下面的例子來說明行 -

ID   Function type  Service Type 
    1     2    3 
    2     4    5 
    3     7    8 

這在Yii框架,而不是在MySQL的水平來實現。我如何在保存之前進行此驗證。請在此引導我。

+0

保存之前,請檢查它是否存在。如果是這樣,不要做任何事情。如果沒有,保存。你錯過了什麼? –

+0

@HamzaKubba我可不可以做模特課。我想你是問我在控制器中做到這一點。我正確.. – user2770039

+0

要麼工作...我主要問你什麼問題是... –

回答

0

型號:

function rules(){ 
     ... // Other rules 
     array('service_type','validate_unique_types'), 

} 

public function validate_unique_types($attribute, $params){ 

     if(!empty($this->function_type) && !empty($this->service_type)){ 
      $count = YourModel::model()->count('function_type = :function_type AND service_type = :service_type',array(':function_type'=>$this->function_type, ':service_type'=>$this->service_type)); 
      if($count){ 
       $this->addError($attribute, "repeated!!"); 
      } 
     } 

} 

如果用戶輸入了「service_tipe」和「函式」中,「validate_unique_types」功能控制,如果存在與這兩個值在DB任何REG。

+0

我得到這個錯誤:類Groupzservicelookup的對象無法轉換爲字符串 – user2770039

+0

錯誤行?證實 ? model() - > count()... –

+0

固定...它的工作原理 – user2770039

相關問題