yii - how to check already exist entry in database -


my default controller function

    public function actionaddnewcategories() {           $model = new addnewcategory();          $model->category_name=strip_tags(trim($_post['categoryname']));          $model->category_description=strip_tags(trim($_post['categorydescription']));          $model->save(false);            $category_list=invoice::getcategoryname();          $test="";          $test = ' <option value="">select category</option>';            foreach($category_list $value ){                 $test .= "<option >{$value['category_name']}</option>";              }          echo $test;     } 

model function

public function getcategoryname() {      $id = yii::app()->db->createcommand()             ->select('category_name')             ->from('add_new_category c')             ->queryall();     return $id; } 

you can add unique rule addnewcategory model below:

array('fieldname','unique','classname'=>__class__,'attributename'=>'columnname','allowempty'=>false) 

by now, have rule, denies insert new record existing value.

another alternative way using exist() below:

$exist=addnewcategory::model()->exist(array('columnname'=>'value')); 

which $exist variable holds boolean value means whether record exists entered condition or not.

http://www.yiiframework.com/doc/api/1.1/cuniquevalidator

http://www.yiiframework.com/doc/api/1.1/cactiverecord#exists-detail


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -