简体   繁体   English

zend_db 的问题

[英]Problem with zend_db

ZF/PHP This is my class Votes: ZF/PHP 这是我的 class 投票:

class Votes extends Zend_Db_table {

protected $_name = 'votes';

public function vote($object_id, $user_id, $vote){

    $data = array('object_id' => $object_id, 'user_id' => $user_id, 'value' => $vote);
    $this->insert($data);

    return true;

 }
} 

The 'votes' has 'id' primary key. 'votes' 有 'id' 主键。 I get: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' when i call vote.我得到:完整性约束违规:1062 当我调用投票时,键“PRIMARY”的重复条目“0”。 Which means the engine every time try to make an insert with '0' as id's value.这意味着引擎每次都尝试使用 '0' 作为 id 值进行插入。 How to force insert to automatically increment 'id' column?如何强制插入自动增加“id”列?

Make the Vote table id, an auto increment field.将投票表 ID 设为自动递增字段。 This should solve the problem.这应该可以解决问题。

Do not add the id - ensure the field type in the database is set to auto-increment and also set it as primary key using不要添加 id - 确保数据库中的字段类型设置为自动增量,并将其设置为主键使用

 protected $_primary

The id field in the database should be defined to auto increment.应将数据库中的 id 字段定义为自动递增。

In MySql the syntax is like the following:在 MySql 中,语法如下:

id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM