Search code examples
zend-frameworkzend-db

Zend Framework - Issue with delete from database code


In my Application_Model_DbTable_User, I have the following function:

public function deleteUser($username)
{
    $this->delete('username = ' . (string) $username);
}

This function is being called from my AdminController, with this three lines of code.

$uname = $this->getRequest()->getParam('username');
$user = new Application_Model_DbTable_User();
$user->deleteUser($uname);

This error however, turns up.

Column not found: 1054 Unknown column 'test' in 'where clause'

With test being the user I am trying to delete.

This code is adapted from a previous code which deletes based on id, a INT field, which works perfectly fine. What am I doing wrong? I would be happy to give more detailed codes if needed. Thanks.


Solution

  • Your query isn't quoted:

    $this->delete('username = ' . (string) $username);
    

    This equates to:

    WHERE username = test
    

    If you use the where() method, it will do this for you:

    $table->where('username = ?', $username);
    

    Or (like the example in the docs):

    $where = $table->getAdapter()->quoteInto('bug_id = ?', 1235);
    $table->delete($where);