2

通过 Symfony 原则更新表时,我偶尔会收到“违反完整性约束:1062 Duplicate entry ... for key 'PRIMARY'”错误。该表是使用相应的历史表创建的。错误不是来自更新表本身,而是来自在历史表中插入记录。我正在使用 Symfony 1.4,学说 1.2。知道是什么原因造成的吗?谢谢。

$this->computer = $computerTable->findOneByMacAddress($this->props['mac_address']);
$this->computer->ip_address = $this->ip;
$this->computer->setLastCheckinAt(date('Y-m-d H:i:s')) ;
$this->computer->save();

schema.yml
Computer:
    actAs:
    Timestampable: ~
    History:
        className: %CLASS%History
        auditLog: true
        deleteVersions: false
        cascadeDelete: false
columns:
    mac_address:            { type: string(13), notnull: true, }
    last_checkin_at:        { type: string(60), }
    ip_address:             { type: string(40), fixed: false, notnull: false, }
    ...
4

1 回答 1

2

在某些情况下很可能findOneByMacAddress找不到现有记录,并且您有一个要保存到数据库中的空列 mac 地址。

尝试这样的事情:

if(!$this->computer = $computerTable->findOneByMacAddress($this->props['mac_address']))
{
   $this->computer = new Computer();
}
/*do logic here*/
$this->computer->save();
于 2012-06-15T22:17:53.607 回答