check existing record before create and update

Hello,

I am new in yii… i am trying to check duplication record in create and update.

My table schema :

[b]Attributes tbl :


	id, name[/b]

[b]

Project tbl :


	id, name, proce[/b]

[b]

Project_attribute_price tbl :


	id,attribute_id,project_id,price[/b]

i am trying to validation if project_id and attribute_id is same in create and update project_attribute_price.

My project_attribute_price model is :

class ProjectAttributesPrice extends CActiveRecord

{

public static function model($className=__CLASS__)


{


	return parent::model($className);


}





public function tableName()


{


	return 'tblproject_attributes_price';


}





public function rules()


{


	return array(


		array('project_id, attribute_id, price', 'required'),


		array('project_id, attribute_id', 'numerical', 'integerOnly'=>true),


		array('price', 'numerical'),


		array('id, project_id, attribute_id, price', 'safe', 'on'=>'search'),


	);


}


public function relations()


{


	return array(


		'projects'=>array(self::BELONGS_TO, 'Projects', 'project_id'),


		'attribute'=>array(self::BELONGS_TO, 'Attributes', 'attribute_id'),


	);


}





public function attributeLabels()


{


	return array(


		'id' => 'ID',


		'project_id' => 'Project',


		'attribute_id' => 'Attribute',


		'price' => 'Price',


	);


}


public function search()


{


	$criteria=new CDbCriteria;





	$criteria->compare('id',$this->id);


	$criteria->compare('project_id',$this->project_id);


	$criteria->compare('attribute_id',$this->attribute_id);


	$criteria->compare('price',$this->price);





	return new CActiveDataProvider($this, array(


		'criteria'=>$criteria,


	));


}

}

Thanks in advance…

what do you mean by checking for a duplication record in update? I see the logic to check for a duplicate in create but not update. Updating means you are altering an existing record and not sure why you need to check for duplicate…I am missing something. In create action however, you can create your own validation rule such as:




public function rules()

{

array('fieldname', 'checkDuplicate', 'on'=>'update'),

}


public function checkDuplicate()

{

...check for duplication here

}



You may also want to check this extension: http://www.yiiframework.com/extension/composite-unique-key-validatable. I haven’t used it but looking at the decent number of likes <_< it may prove worth reading it.

Hello bettor,

thanks for reply… i am trying to show you in example why i want to check in update also :

table : project_attribute_price


	


	id	project_id	attribute_id	price


	1	2			3			300


	2	2			1			400


	3	1			3			150

In following example if by mistake i update last record (id 3) into project_id=2, attribute_id=3, In the database look like after update

	id	project_id	attribute_id	price


	[b]1	1			2			300[/b]


	2	2			1			400


	3	1			3			150

That why i want to check below update

See below example in Core Php :

$check=mysql_query("select id from project_attribute_price where project_id=post_project_id AND attribute_id=post_attribute_id AND id<>post_id,$conn);

if(mysql_num_rows($check)>0)

{

return false;

}else{

return true;

}

Understood! Then the above suggestions should work for you. Please try them and post if you hit into issues. Good luck

I think you could also look at using beforeSave() and if duplicate change an insert into a update. I have never done this but I think it depends on your workflow. validate vs beforeSave

http://www.yiiframework.com/wiki/10/how-to-automate-timestamps-in-activerecord-models/

EG:


public function beforeSave() {

  $duplicate = $this->checkDuplicate()

    if ($this->isNewRecord && $duplicate != FALSE) {

      $this->id = $this->$duplicate;

      $this->isNewRecord = FALSE;

    }

 

    return parent::beforeSave();

}

I’m also interested in this solution. Have you found something?

isn’t it possible that somehow this can be used?

UPDATE: for me this seems to work.