Search code examples
zend-frameworkzend-db

Zend duplicated rows on mysql insert


For some reason, when I do an mysql db insert from Zend, my row is dulpicated. I've tried a direct insert via phpmyadmin and it works perfect, so its not a mysql server problem.

This is the code I use:

<?php

class Model_Team extends Zend_Db_Table_Abstract {

    protected $_name = 'team';

    public function createUser($data) {
        $user = $this->createRow();
        $user->name = $data['name'];
        $user->title = $data['title'];
        $id = $user->save();
        return $id;
    }
}

?>

Thanks in advance.

EDIT:

I've found that this duplication only occurs when i call the form via AJAX (modal box), although the form post is normal, not an ajax request)


Solution

  • I don't know why your code is double pumping the database on save but it should'nt matter as you're using the Row object and save(). (save() inserts or updates)
    You may want to restructure your createUser() function so that it can't create a new row if the row already exists.

    <?php
    
    class Model_Team extends Zend_Db_Table_Abstract {
    
    protected $_name = 'team';
    
    public function createUser(array $data) {
    
        $user = $this->createRow();
        //test if user has id in the array
        if (array_key_exists('id', $data)){
            $user->id = $data['id'];
        }
        $user->name = $data['name'];
        $user->title = $data['title'];
        $user->save();
        //no need to create a new variable to return the user row
        return $user;
       }
    }
    

    This method will create and update a user row.

    To help you further I'll need to see the controller code most of my double pumps have happened there.