0
votes

I have a mysql table named users having fields: username, password, email I have controller/action like this 'user/update' for adding new user and 'user/update/id/{user_id}' I have same Zend_Form for both controller. For adding new user, I've checked whether the username already exist or not using below code:

$username->addValidator('Db_NoRecordExists', true, array('table' => 'user', 'field' => 'username'));

This works well when new user is added but when editing the user, even when the user leaves the username field as it is, i get username already exist error. Is their a validation that zend provide similar to Db_NoRecordExists for editing the field. In editing case,i want query like:

SELECT COUNT(*) FROM users WHERE username='$username' AND id!=$update_id

How can i do this?

2

2 Answers

0
votes

Zend_Validate_Db_NoRecordExists has an exclude option for this case:

$validator = new Zend_Validate_Db_NoRecordExists(
    array(
        'table' => 'users',
        'field' => 'username',
        'exclude' => array(
            'field' => 'id',
            'value' => $user_id
        )
    )
);
1
votes

You can convert your sql query into Zend_Db_Select

$select = Zend_Db_Table::getDefaultAdapter()->select()->from('users')->where('username =?',$username)
                                                                    ->where('id != ?',$update_id);


$validator =  new Zend_Validate_Db_NoRecordExists($select);