MGP
MGP

Reputation: 653

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)

Upvotes: 2

Views: 1078

Answers (2)

RockyFord
RockyFord

Reputation: 8519

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.

Upvotes: 1

Richard Parnaby-King
Richard Parnaby-King

Reputation: 14862

Instead of using createRow() have you tried using insert()?

/**
 * Insert array of data as new row into database
 * @param array $data associative array of column => value pairs.
 * @return int Primary Key of inserted row
 */
public function createUser($data)
{
    return $this->insert($data);
}

Also - could we see the ajax code? It may be that the form is being posted as well?

Upvotes: 0

Related Questions