Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

How to update a record with field as a variable

posted by aurelien 10 years ago

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

(I know this is an old question, but I wanted to post for future users)

fActiveRecord is built in a way that you call setFieldName() and that in turn calls ->set('field_name'). This allows individual classes to override the default behavior for a single column.

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

If you need to do it dynamically, you may want to write something such as:

$control = new Control($name);
$fields = array('name', 'date');
foreach ($fields as $field) {
    $method = 'set' . fGrammar::camelize($field, TRUE);
    $control->$method($value);
}
$control->store();

I'm hoping to introduce a new construct on fActiveRecord soon that will make things like this simpler.

posted by wbond 8 years ago