Open tablecell opened 7 years ago
Check your entity definition. You probably forgot to mark 'uid' as a unique field there:
class Contact extends Spot\Entity
{
protected static $table = 'contact';
public static function fields()
{
return [
'id' => ['type' => 'integer', 'primary' => true, 'autoincrement' => true],
'uid' => ['type' => 'integer', 'default' => 0, 'unique' => true],
'address' => ['type' => 'text']
];
}
}
field uid without unique index
CREATE TABLE
contact
(id
int(10) unsigned NOT NULL AUTO_INCREMENT,uid
int(255) NOT NULL,address
varchar(255) COLLATE utf8_unicode_ci DEFAULT '') ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_cinothing happened
add new row with (id=5,uid=14, address=xxx)
repeat
add new row with (id=6,uid=14, address=xxx)
(id=5,uid=14, address=xxx) changed to (id=5,uid=14, address=yyyy )
field uid with unique index CREATE TABLE contact ( id int(10) unsigned NOT NULL AUTO_INCREMENT, uid int(255) NOT NULL, address varchar(255) COLLATE utf8_unicode_ci DEFAULT , UNIQUE KEY
uid
(uid
)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
nothing happened
add new row (id=7,uid=24,address=aaaa)
repeat
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '24' for key 'uid'' in
(id=7,uid=14, address=aaaa) changed to (id=7,uid=24, address=bbbb )
conclusion: if first param $data include uid then insert new record first param $data exclude uid then update record
not automatic as expect