0

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 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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
        )
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.