How to update a record with field as a variable / Help Please!

How to update a record with field as a variable

Hello,

Here is a piece of code that fails :

// load record
    $control = new Control($name);
    // update record
    $control->set($field, $value);
    // save record
    $control->store();

Because 'set' is a protected method of an fActiveRecord.

However, I dont know what alternative to use. In this block, I dont know the field to update, I cannot do, for example, method 'setName' because field may be 'name' but maybe 'duration', etc…

Thanks for your help !

Aurelien

  • Message #517

    Hei, i don't know what exactly you want but try this: $control→{$field}($value);.

    • Message #518

      corect:

      $control{'set'.$field}($value);.
      
      • Message #520

        Thank you Mungiu !

        I was actually trying something like that when i read your message.

        In the end I also needed a :

        $camelfield = fGrammar::camelize($field, TRUE);
        

        And then

        $control->{"set" . $camelfield}($value);
        

        It workd but I dont like it, it is not sexy