开发者

symfony: how to loop through fields of Doctrine object

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_stre开发者_如何学Goet','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?


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()) {
  // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜