silverstripe

Using the ORM

Reading and writing DataObjects

DataObjects in SilverStripe represent a database table row. The fields in the model have magic methods that handle getting and setting data via their property names.

Given we have a simple DataObject as an example:

class Fruit extends DataObject
{
    private static $db = ['Name' => 'Varchar'];
}

You can create, set data and write a Fruit as follows:

$apple = Fruit::create();
$apple->Name = 'Apple';
$apple->write();

You can similarly retrieve the Fruit object as follows:

$apple = Fruit::get()->filter('Name', 'Apple')->first();
var_dump($apple->Name); // string(5) "Apple"

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow