1

I need to loop through a list of fields in a custom validator to compare the value against a value already stored in the database.

My code here:

$healthUser = PersonTable::getInstance->getHealthUser(trim($values['nhi']));

if ($healthUser->getNHI() == trim($values['nhi']) &&
$healthUser->getName() != trim($values['name'])){

//Also loop through all fields and show differences
foreach (array('suite','hnr_street','suburb','city','postcode','postal_address') 
         as $field){

     if ($value[$field] != $healthUser->getFieldName()){
//How do I get the field name from $field?--^^^^^^^^^^


         $errorSchemaLocal->addError(new sfValidatorError($this, 
                                     'fieldIsDifferent', $healthUser->getFieldName()),
                                     $field);
     }
 }

SO basically i need to create the getter function from the field name in $field.

Any idea how to do this?

jdog
  • 2,465
  • 6
  • 40
  • 74

1 Answers1

1

Doctrine record implements ArrayAccess interface. You can simply access the record as an array:

if ($value[$field] != $healthUser[$field]) {
  // ...
}

You can also use sfInflector to construct a getter name:

$getField = sprintf('get%s'), ucfirst(sfInflector::cammelize($field)));
if ($value[$field] != $healthUser->$getField()) {
  // ...
}
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • Cool! I didn't know about the sfInflectorCammelize function. Very useful indeed. thanks. – Flukey Jan 10 '11 at 16:27
  • ArrayAccess doesn't work for me - is this version specific? Otherwise happy with using inflector function. – jdog Jan 17 '11 at 02:58
  • Nope, it's nothing special. It's often a standard way of coping with model in the documentation: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/working-with-models/en#dealing-with-relations:creating-related-records – Jakub Zalas Jan 17 '11 at 06:44