Yii Framework Forum: Unique Records - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Unique Records Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 21 April 2010 - 06:17 AM

I am trying to implement unique records in the database table. The fields product_id and image_name should be unique as a combination - how do I specify a combination of fields to check on?

I also need to pass back an error message to the view (maybe using $model->addError)?
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 21 April 2010 - 06:24 AM

this should help you
http://www.yiiframew...alidation-rules
0

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 21 April 2010 - 06:33 AM

OK I tried array('product_id, image_name', 'unique')

But this seems to check each attribute seperately - I need it to check as a combination.
0

#4 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 21 April 2010 - 06:57 AM

if one is unique, the combination is unique, too ;)
0

#5 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 21 April 2010 - 07:02 AM

no man. for example - current data:

product_id | image_name

001 | flowers.jpg


input data:

001 | animals.jpg (this is OK)

002 | flowers.jpg (this is OK)

001 | flowers.jpg (this is not OK)
0

#6 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 21 April 2010 - 07:02 AM

public function rules()
    {
        return array(
            array('image_name', 'checkCombination'),
        );
    }
 
    public function checkCombination($attribute,$params)
    {
        if(!$this->hasErrors())  // we only want to authenticate when no input errors
        {
            $product = Product::model()->find('product_id = ? AND image_name = ?', array($this->product_id, $this->image_name));
            if($product)
                $this->addError('image_name','Too Bad.');
        }
    }


0

#7 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 21 April 2010 - 08:21 AM

cheers mbi - that kind of half works - it does not add the duplicate entry to the database but the error is not displayed on the page. I have put <?php echo CHtml::errorSummary($model); ?> on the page.
0

#8 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 21 April 2010 - 10:58 AM

BUMP

$this->addError('image_name','Too Bad.');

also seems to be causing problem with updating the record.
0

#9 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 21 April 2010 - 11:20 AM

Cause the rule
array('image_name', 'checkCombination')


applies for all the scenarios.

You can specify the 'on' to say in wich scenarios the rule must apply:
array('image_name', 'checkCombination', 'on'=>'insert')


Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#10 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 22 April 2010 - 03:53 AM

Thanks PoL, but I still can't get the error message to display on the page, any idea why?
0

#11 User is offline   _wk_ 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 22-January 10

Posted 22 April 2010 - 09:16 AM

View PostGSTAR, on 22 April 2010 - 03:53 AM, said:

Thanks PoL, but I still can't get the error message to display on the page, any idea why?


//.. in your view
<?php echo CHtml::errorSummary($model); ?>


Sounds simple but has caught me out a few times...
0

#12 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,037
  • Joined: 30-October 09
  • Location:UK

Posted 22 April 2010 - 09:24 AM

View Postkevinb, on 22 April 2010 - 09:16 AM, said:

//.. in your view
<?php echo CHtml::errorSummary($model); ?>


Sounds simple but has caught me out a few times...

I already got that bro, error message still does not display...
0

#13 User is offline   _wk_ 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 22-January 10

Posted 22 April 2010 - 09:25 AM

View Postkevinb, on 22 April 2010 - 09:16 AM, said:

//.. in your view
<?php echo CHtml::errorSummary($model); ?>


Sounds simple but has caught me out a few times...


Sorry, just noticed you specified this in an eralier post...

Try verifying the model attributes are getting assigned e.g.

//..
$model->attributes = $_POST['TheForm'];
CVarDummper::dump($model->attributes);


Or go inside your custom validator and set some echo points to see if it is working.
0

Share this topic:


Page 1 of 1
  • 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