Yii Framework Forum: How to validate composite unique key? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to validate composite unique key? Rate Topic: -----

#1 User is offline   ricardograna 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 376
  • Joined: 31-March 09
  • Location:Manaus/AM - Brazil

Posted 15 June 2009 - 07:47 AM

My table have the following informations: company, year and number. All them are part of a unique key.

I know how to validate a basic unique key, but how can I make rules() to validate my key?

Thx!
0

#2 User is offline   Dave 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 187
  • Joined: 09-October 08

Posted 15 June 2009 - 09:45 AM

You can change the return value of the rules method of your model.


public function rules()
    {
        return array(
            array('attriubteThatNeedsToBeUnique', 'myValidator'),
    );
    }

    public function myValidator($attribute,$params)
    {
        //do your check here
    }


More information you can find in the documentation: http://www.yiiframew...uide/form.model
0

#3 User is offline   ricardograna 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 376
  • Joined: 31-March 09
  • Location:Manaus/AM - Brazil

Posted 15 June 2009 - 12:26 PM

In Yii, we have

public function rules()
    {
        return array(
            array('myAttribute', 'unique'),
    );
}



I am thinking that Yii have somehing like

public function rules()
    {
        return array(
            array(array('attr1', 'attr2', 'attr3'), 'unique'),
    );
}



But this don't work.
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 15 June 2009 - 01:44 PM

The uniqueness check for composite key is not supported currently. We may consider implementing it in 1.1. Could you please create a ticket for this? Right now, you will need to implement it by yourself using either a method-based or class-based validation.
0

#5 User is offline   ricardograna 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 376
  • Joined: 31-March 09
  • Location:Manaus/AM - Brazil

Posted 15 June 2009 - 02:00 PM

Ok, thanks Qiang!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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