2012-03-12 14 views
31

मेरे पास इस तरह की एक तालिका है: (आईडी, नाम, संस्करण, टेक्स्ट)। (नाम, संस्करण) अद्वितीय कुंजी है, मैं इसे सत्यापित करने के लिए नियम कैसे बना सकता हूं।yii: दो गुणों के लिए एक अद्वितीय नियम कैसे बनाएं

उत्तर

48

यह Yii से ही किया जा सकता है, आप इसके लिए एक विस्तार की जरूरत नहीं है। हालांकि एक विस्तार rules() विधि की सफाई के रूप में यहाँ वर्णित मदद कर सकते हैं:

public function rules() { 
    return array(
     array('firstKey', 'unique', 'criteria'=>array(
      'condition'=>'`secondKey`=:secondKey', 
      'params'=>array(
       ':secondKey'=>$this->secondKey 
      ) 
     )), 
    ); 
} 

में:

http://www.yiiframework.com/extension/unique-attributes-validator/

इस कोड (उस साइट से नकल) जो एक्सटेंशन का उपयोग कर के बिना काम करेगा $this->secondKey का मान rules() के अंदर उपलब्ध नहीं है - विधि आप CACTiveRecords beforeValidate() में मान्यकर्ता जोड़ सकते हैं- इस तरह की विधि:

public function beforeValidate() 
{ 
    if (parent::beforeValidate()) { 

     $validator = CValidator::createValidator('unique', $this, 'firstKey', array(
      'criteria' => array(
       'condition'=>'`secondKey`=:secondKey', 
       'params'=>array(
        ':secondKey'=>$this->secondKey 
       ) 
      ) 
     )); 
     $this->getValidatorList()->insertAt(0, $validator); 

     return true; 
    } 
    return false; 
} 
+0

मैंने इस विधि को आजमाया, लेकिन मुझे मूल्य '$ यह नहीं मिल रहा है-> secondKey' – Khaleel

+0

'$ this-> secondKey' दूसरी विशेषता होना चाहिए जो अद्वितीय होना चाहिए। आपको इसे अपने विशेषता नाम में समायोजित करना होगा। – cebe

+0

मुझे पता है कि। मैंने अपने विशेषता नाम के साथ प्रयास किया। मुझे सिर्फ नियमों() ' – Khaleel

-5

हो सकता है आप अपने कोड पर इस rules जोड़ सकते हैं

return array(
    array('name', 'unique', 'className'=>'MyModel', 'attributeName'=>'myName'), 
    array('version', 'unique', 'className'=>'MyModel', 'attributeName'=>'myVersion') 
); 
+3

यह केवल मान्य करेगा कि नाम अद्वितीय है और संस्करण अकेले अद्वितीय है । यह स्तंभों (नाम, संस्करण) 'की जोड़ी को मान्य नहीं करता है। – cebe

7

आपको नियमों() विधि और तृतीय पक्ष एक्सटेंशन की जटिल सामग्री की आवश्यकता नहीं है। बस अपनी खुद की सत्यापन विधि बनाएँ। इसे अपने आप करना बहुत आसान है।

public function rules() 
{ 
    return array(
    array('firstField', 'myTestUniqueMethod'), 
); 
} 

public function myTestUniqueMethod($attribute,$params) 
{ 

    //... and here your own pure SQL or ActiveRecord test .. 
    // usage: 
    // $this->firstField; 
    // $this->secondField; 
    // SELECT * FROM myTable WHERE firstField = $this->firstField AND secondField = $this->secondField ... 
    // If result not empty ... error 

    if (!$isUnique) 
    { 
    $this->addError('firstField', "Text of error"); 
    $this->addError('secondField', "Text of error"); 
    } 

} 
+0

+1 सरल और आसान :) – AlphaMale

4

वे (अभी तक एक) समाधान Yii1.14rc की अगली रिलीज उम्मीदवार में अद्वितीय समग्र कुंजी के लिए समर्थन जोड़ा है, लेकिन यहाँ है। बीटीडब्लू, यह कोड उन नियमों में समान 'विशेषता नाम' का उपयोग करता है जो वाईआई ढांचे अगले आधिकारिक रिलीज में उपयोग करेंगे।

संरक्षित/मॉडल/शासन की शुरुआत में Mymodel.php

public function rules() 
    { 
     return array(
      array('name', 'uniqueValidator','attributeName'=>array(
       'name', 'phone_number','email') 
     ), 
... 
  • 'नाम' विशेषता प्रमाणीकरण त्रुटि के साथ संलग्न किया जाएगा और अपने फार्म पर बाद में उत्पादन होता है।
  • 'विशेषता' नाम (सरणी) में कुंजी की एक सरणी होती है जिसे आप संयुक्त कुंजी के रूप में एक साथ मान्य करना चाहते हैं।

संरक्षित/घटकों/प्रमाणकों/uniqueValidator.php

class uniqueValidator extends CValidator 
    { 
     public $attributeName; 
     public $quiet = false; //future bool for quiet validation error -->not complete 
    /** 
    * Validates the attribute of the object. 
    * If there is any error, the error message is added to the object. 
    * @param CModel $object the object being validated 
    * @param string $attribute the attribute being validated 
    */ 
    protected function validateAttribute($object,$attribute) 
    { 
     // build criteria from attribute(s) using Yii CDbCriteria 
     $criteria=new CDbCriteria(); 
     foreach ($this->attributeName as $name) 
      $criteria->addSearchCondition($name, $object->$name, false ); 

     // use exists with $criteria to check if the supplied keys combined are unique 
     if ($object->exists($criteria)) { 
      $this->addError($object,$attribute, $object->label() .' ' . 
       $attribute .' "'. $object->$attribute . '" has already been taken.'); 
     } 
    } 

} 

के रूप में आप जितने चाहें उतने विशेषताओं का उपयोग कर सकते हैं और यह आपके CModels के सभी के लिए काम करेंगे। चेक "मौजूद" के साथ किया जाता है।

संरक्षित/config/main.php

'application.components.validators.*', 

आप ताकि uniqueValidator.php Yii आवेदन के द्वारा पाया जाएगा 'आयात' श्रेणी में अपने config करने के लिए उपरोक्त पंक्ति जोड़ने के लिए हो सकता है।

सकारात्मक प्रतिक्रिया और परिवर्तन बहुत स्वागत है!

+1

मैं अभी भी एक बहुत बड़े ऐप के लिए 1.13 पर हूं कि मेरे पास अपग्रेड करने का समय नहीं है। एक त्वरित परीक्षण से, यह बहुत अच्छा काम करता है, धन्यवाद! यह या तो कस्टम वैल्यूएटर बनाने के लिए भी बचाता है (कड़ी मेहनत नहीं है, लेकिन गन्दा हो सकता है) और दूसरी समस्या के रूप में गतिशील मान को पार करने के साथ कुछ मुद्दों को हल करता है (_really_ messy प्राप्त कर सकते हैं)। – ldg

-2

यह बहुत आसान है। अपने सरणी में, अपने एक्सटेंशन वर्ग में बनाए गए एक param को शामिल करें।

अगला कोड मॉडल के अंदर है।

array('name', 'ext.ValidateNames', 'with'=>'lastname') 

अगला कोड एक्सटेंशन ValidateNames से एक्सटेंशन फ़ोल्डर में है।

class ValidateNames extends CValidator 
{ 
    public $with=""; /*my parameter*/ 

    public function validateAttribute($object, $attribute) 
    { 
     $temp = $this->with; 
     $lastname = $object->$temp; 
     $name = $object->$attribute; 
     $this->addError($object,$attribute, $usuario." hola ".$lastname); 
    } 
} 
+1

आपके उत्तर में ऐसी कोई जानकारी नहीं है जो कम से कम किसी अन्य उत्तर द्वारा प्रदान नहीं की गई है। –

3

Yii1:

http://www.yiiframework.com/extension/composite-unique-key-validatable/

Yii2:

// a1 needs to be unique 
['a1', 'unique'] 
// a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value 
['a1', 'unique', 'targetAttribute' => 'a2'] 
// a1 and a2 need to be unique together, and they both will receive error message 
[['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']] 
// a1 and a2 need to be unique together, only a1 will receive error message 
['a1', 'unique', 'targetAttribute' => ['a1', 'a2']] 
// a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value) 
['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']] 

http://www.yiiframework.com/doc-2.0/yii-validators-uniquevalidator.html

3

Yii2 में:

public function rules() { 
    return [ 
     [['name'], 'unique', 'targetAttribute' => ['name', 'version']], 
    ]; 
} 
0

ऊपर समारोह के आधार पर, यहाँ एक समारोह है आप अपने ActiveRecord मॉडल पर जोड़ सकते हैं

आप यह इतना तरह का प्रयोग करेंगे,

array(array('productname,productversion'), 'ValidateUniqueColumns', 'Product already contains that version'), 


/* 
* Validates the uniqueness of the attributes, multiple attributes 
*/ 
public function ValidateUniqueColumns($attributes, $params) 
{ 
    $columns = explode(",", $attributes); 
    //Create the SQL Statement 
    $select_criteria = ""; 
    $column_count = count($columns); 
    $lastcolumn = ""; 

    for($index=0; $index<$column_count; $index++) 
    { 
     $lastcolumn = $columns[$index]; 
     $value = Yii::app()->db->quoteValue($this->getAttribute($columns[$index])); 
     $column_equals = "`".$columns[$index]."` = ".$value.""; 
     $select_criteria = $select_criteria.$column_equals; 
     $select_criteria = $select_criteria." "; 
     if($index + 1 < $column_count) 
     { 
      $select_criteria = $select_criteria." AND "; 
     } 
    } 

    $select_criteria = $select_criteria." AND `".$this->getTableSchema()->primaryKey."` <> ".Yii::app()->db->quoteValue($this->getAttribute($this->getTableSchema()->primaryKey)).""; 

    $SQL = " SELECT COUNT(`".$this->getTableSchema()->primaryKey."`) AS COUNT_ FROM `".$this->tableName()."` WHERE ".$select_criteria; 

    $list = Yii::app()->db->createCommand($SQL)->queryAll(); 
    $total = intval($list[0]["COUNT_"]); 

    if($total > 0) 
    { 
     $this->addError($lastcolumn, $params[0]); 
     return false; 
    } 

    return true; 
} 
संबंधित मुद्दे