简体   繁体   中英

Problem with zend_db

ZF/PHP This is my class Votes:

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. I get: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' when i call vote. Which means the engine every time try to make an insert with '0' as id's value. How to force insert to automatically increment 'id' column?

Make the Vote table id, an auto increment field. 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

 protected $_primary

The id field in the database should be defined to auto increment.

In MySql the syntax is like the following:

id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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