Yii Framework Forum: composite unique key validation - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

composite unique key validation Rate Topic: -----

#21 User is offline   oriol 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 27-September 11

Posted 27 September 2011 - 03:52 AM

Hi.
First of all, thanks for this extension. I've started using it and I found something that maybe can be useful.
I've need to validate an unique restriction with null values. For example:

Id Value1 Value2
#1 A B (not valid)
#2 A C
#3 A B (not valid)
#4 B A
#5 B null (not valid)
#6 B null (not valid)

To support this feature I've modified the extension code as follow:

In the function validateCompositeUniqueKeys I've changed
$criteria = new CDbCriteria();
foreach ($uk['attributes'] as $attr) {
    $criteria->compare($attr, $object->$attr);
}

for
$criteria = new CDbCriteria();
foreach ($uk['attributes'] as $attr) {
  if ($object->$attr === null)
    $criteria->addCondition($attr.' is null');
  else
    $criteria->compare($attr, $object->$attr);
}

2

#22 User is offline   ololo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 23-January 11
  • Location:Minsk, Belarus

Posted 27 September 2011 - 03:27 PM

thank you for sharing, I've updated the extension

btw, validation behavior may also look strange when validating composite keys with empty strings (see CDbConnection::nullConversion and CDbCriteria::compare)
0

#23 User is offline   G_Gus 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 29-August 11

Posted 22 November 2011 - 05:21 AM

View Postjeremy, on 07 June 2011 - 01:12 PM, said:

[...] to track the oldAttributes. I already have this in place in my application, for automatic changeLog creation:

class MyActiveRecord extends CActiveRecord {
	public $_oldAttributes = array();

	public function afterFind() {
    	$this->setOldAttributes($this->getAttributes());
	}

	public function getOldAttributes() {
    	return $this->_oldattributes;
	}

	public function setOldAttributes($value) {
    	$this->_oldattributes = $value;
	}
 }



Watch out, that won't work without correcting the camel case: $this->_oldattributes should be $this->_oldAttributes
0

#24 User is offline   Pep 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 06-August 11

Posted 21 April 2012 - 03:34 AM

I find that the built in CUniqueValidator is more than enough to handle this task. It is very flexible and you can even use relations to verify the uniqueness of an attribute. The default error message is appropriate but you can customize it to taste.

For example, an application can have many users and each user can have their own invoice numbers. However, you want the users not to use duplicate invoice numbers from their own account. In this example the unique composite key would consist of the "invoice_number" attribute and the "user_id" attribute through the relation "account". (You could of course have the user_id attribute in the invoice model but I wanted to demonstrate the use of relations along with the use of validating unique composite keys.) Your rule in the invoice model could look like this:

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
                array('invoice_number', 'unique', 'caseSensitive'=>false, 'criteria'=>array(
                                'condition'=>'account.user_id = :id',
				'params'=>array(':id'=>Yii::app()->user->id),
				'with'=>'account',
			),
			'message'=>'{attribute} "{value}" is already in use.'),
	);
}

0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users