require_once 'dataobject.php'; $db = DB::connect("mysql://user:password@localhost/mydatabase"); $person = new DataObject($db, 'person'); $person->find(); // query the database outside of DataObject $pdo = $db->prepare("INSERT INTO person(name, age) values(?, ?)"); $pdo->execute(["John", 25]); // flush and destroy cache $person->flush_and_destroy_cache(); // re-execute the query $person->find();
class Person extends DataObject { function __construct() { $db = DB::connect("mysql://user:password@localhost/mydatabase"); parent::__construct($db, 'person'); } function insert($name, $age) { $pdo = $this->database()->prepare("INSERT INTO person(name, age) values(?, ?)"); $pdo->execute([$name, $age]); $this->flush_and_destroy_cache(); } } $person = new Person(); $person->insert("John", 25);In this example, a custom class is created based on the DataObject, and a new insert method is added. The cache is flushed and destroyed after the insert operation to prevent incorrect results from being returned. The package library for PHP DataObject is PEAR.