Zend_Validate_Db_RecordExists vs 2 fields

I usually use Zend_Validate_Db_RecordExists to update or insert a record. This works fine with a single check field. How to do this if you have two fields to check?

$validator = new Zend_Validate_Db_RecordExists( array( 'table' => $this->_name, 'field' => 'id_sector,day_of_week' ) ); if ($validator->isValid($fields_values['id_sector'],$fields_values['day_of_week'])){ //true } 

I tried it with an array and a comma separated list, nothing works ... Any help is appreciated. Regards Andrea

+4
source share
6 answers

To do this, you need to extend the Zend_Validate_Db_RecordExists class.

He currently does not know how to check for multiple fields.

You can simply use two different validation instances to validate the two fields separately. This is the only work that I see now, except for its expansion.

If you decide to expand it, you will have to find a way to pass all the fields to the constructor (the array seems to be a good choice), and then you have to delve into the method that creates the sql query. In this method, you will have to iterate over the array of fields that were passed to the constructor.

+4
source

You should examine the exclude parameter. Something like this should do what you want:

 $validator = new Zend_Validate_Db_RecordExists( array( 'table' => $this->_name, 'field' => 'id_sector', 'exclude' => array( 'field' => 'day_of_week', 'value' => $fields_values['day_of_week'] ) ); 

The exclude field will effectively add to the automatically generated WHERE part to create something equivalent to this:

 WHERE `id_sector` = $fields_values['id_sector'] AND `day_of_week` = $fields_values['day_of_week'] 

Its kind of hack is that we use it for the opposite of what it was intended for, but it works the same way for me (I use it with Db_NoRecordExists ).

Source: Zend_Validate_Db_NoRecordExists Example

+2
source
 $dbAdapter = Zend_Db_Table::getDefaultAdapter(); 'validators' => array('EmailAddress', $obj= new Zend_Validate_Db_NoRecordExists(array('adapter'=>$dbAdapter, 'field'=>'email', 'table'=>'user', 'exclude'=>array('field'=>'email','value'=>$this->_options['email'], 'field'=>'is_deleted', 'value'=>'1') ))), 
+1
source

Sorry for the late reply.

The best option that worked for me is:

 // create an instance of the Zend_Validate_Db_RecordExists class // pass in the database table name and the first field (as usual)... $validator = new Zend_Validate_Db_RecordExists(array( 'table' => 'tablename', 'field' => 'first_field' )); // reset the where clause used by Zend_Validate_Db_RecordExists $validator->getSelect()->reset('where'); // set again the first field and the second field. // :value is a named parameter that will be substituted // by the value passed to the isValid method $validator->getSelect()->where('first_field = ?', $first_field); $validator->getSelect()->where('second_field = :value', $second_field); // add your new record exist based on 2 fields validator to your element. $element = new Zend_Form_Element_Text('element'); $element->addValidator($validator); // add the validated element to the form. $form->addElement($element); 

I hope this helps someone :)

Although I would highly recommend a tidier solution that should extend the Zend_Validate_Db_RecordExists class with the above code.

Enjoy !! Rosario

+1
source

For those using Zend 2, if you want to check if a user exists with the id and email data in the users table, this is possible.

First you create a select object that will be used as a parameter for the Zend\Validator\Db\RecordExists

 $select = new Zend\Db\Sql\Select(); $select->from('users') ->where->equalTo('id', $user_id) ->where->equalTo('email', $email); 

Now create a RecordExists object and check for the existence of this path.

 $validator = new Zend\Validator\Db\RecordExists($select); $validator->setAdapter($dbAdapter); if ($validator->isValid($username)) { echo 'This user is valid'; } else { //get and display errors $messages = $validator->getMessages(); foreach ($messages as $message) { echo "$message\n"; } } 

This sample is from white paper ZF2

0
source

You can use the 'exclude' in this parameter to pass the second sentence that you want to filter.

 $clause = 'table.field2 = value'; $validator = new Zend_Validate_Db_RecordExists( array( 'table' => 'table', 'field' => 'field1', 'exclude' => $clause ) ); if ($validator->isValid('value') { true; } 
0
source

All Articles