how to update data

Silverstripe Version:

Question:

this code will insert. what if want to update Name using the field name as condition?

$d= new TrainingForm();
        $d->Name = 'ray';
        $d->write(); 

Hi… a few different ways to deal with this. In the case of your example, you can just re-assign the value and save the object:

$d= new TrainingForm();
        $d->Name = 'ray';
        $d->write();

$d->Name = 'Venkman';
$d->write();

A few other bits of syntax which might be handy:

//Assign the values as you create the record

$d = TrainingForm::create([
 'Name' => 'Egon',
 'Job' => 'Buster',
 'Location' => 'Zuul'
]);

$d->write();


//Bulk-update the values of a record
$d = TrainingForm->get()->byID(1);  //Get the record any way you want
$d->update([
 'Location' => 'New York',  //update an existing value
 'AfraidOf' => 'NoGhosts'  //add the value
]);
$d->write();

Hope that’s useful!