问题
I have a table below :
+-----+-------+------+------------+ | ID | RefID | Type | EventTime | +-----+-------+------+------------+ | 101 | 228 | 1 | 1437195633 | | 102 | 228 | 5 | 1437195633 | | 103 | 228 | 1 | 1437195633 | | 104 | 228 | 1 | 1437195442 | | 105 | 228 | 1 | 1437195442 | | 106 | 228 | 5 | 1437195442 | | 107 | 228 | 1 | 1437165634 | | 108 | 228 | 5 | 1437165442 | | 109 | 228 | 1 | 1437165634 | | 110 | 228 | 5 | 1437165442 | +-----+-------+------+------------+
In that I want to stop inserting duplicate
data based on the columns RefID,Type,EventTime
only when value of Type = 1
.
In the above table ID
pair is duplicate (101,103), (104,105), (107,109)
.
If now I will insert another data say :
INSERT INTO table VALUES('',228,1,1437165634);
Then it should not insert
. I am checking while inserting into that table but that is not working as I have checked at the same time 2 insert
query is happening, I need to stop it using UNIQUE key
constraints.
回答1:
You need change DB architecture. Add table with unique index by RefId, here you will write record with Type "1". In your Yii model in method beforesave check your Type if is 1 to write in added table otherwise write to old table. And you need change beforefind method
And I'm sorry for my english
回答2:
Please try:
public function unique($attribute, $params)
{
if(!empty($model_name)){
$this->addError('RefID', 'RefID already exists! Please choose a different one');
return true;}
}
as a custom function in before save. and call it for the fields you want in the rule. I'm showing you the template for a single field refID
回答3:
I have solved it by using trigger as below :
DB Trigger
delimiter $$
drop trigger if exists stop_duplicate $$
create trigger stop_duplicate before insert on table
for each row
begin
set @found := false;
if new.Type = 1 then
SELECT
TRUE
INTO @found FROM
table
WHERE
RefID = new.RefID AND EventTime= new.EventTime AND Type= new.Type;
if @found then
signal sqlstate '23000' set message_text = 'CUSTOM_MSG_DUPLICATE';
end if;
end if;
end $$
delimiter ;
Yii Model Code
public function save($runValidation = true, $attributes = null) {
Yii::log(__METHOD__.":: Start ", 'info');
try {
return parent::save();
} catch (Exception $e) {
$errorInfo = $e instanceof PDOException ? $e->errorInfo : null;
$message = $e->getMessage();
Yii::log(__METHOD__ . ": errorcode :{$e->getCode()}, errormessage:{$message} ", 'info');
// Added for handling duplicate entry issue for index
if ((int) $e->getCode() === 23000 && strpos($message, 'CUSTOM_MSG_DUPLICATE') !== false) {
return false;
}
throw new CDbException(Yii::t('yii', 'CDbCommand failed to execute the SQL statement: {error}', array('{error}' => $message)), (int) $e->getCode(), $errorInfo);
}
}
来源:https://stackoverflow.com/questions/32090658/duplicate-data-stop-in-mysql