Skip to content
philcali edited this page Feb 24, 2012 · 5 revisions

The DAO API is a convenience wrapper around common Moodle $DB calls.

Why?

  1. Making global $DB; statements everywhere gets old fast.
  2. Convenience wrapper makes db optimized retrieval explicit and organized.
  3. Largely self-documenting.
  4. Why not? A developer can choose not to use the API entirely, if they so desired.

What's available?

The API consists of mostly public static functions for retrieval convenience.

  • upgrade: Pass in a db anonymous object from one of Moodle $DB->get_record to upgrade to the class of choice.
  • by_id: Retrieves a single object based on numeric id
  • by_sql: When extremely complex sql is required.
  • get: Retrieves a single object based on criterion
  • get_all: Retrieves multiple objects based on criterion
  • delete: Removes a single record
  • delete_all: Removes all records that match criterion
  • count: Returns a number based on criterion
  • update: Mass update records with chosen fields based on criterion
  • save: Stores object (non static)

UES ships with a dao dsl that makes building complex select statements pure PHP.

Examples

$by_philip = array("firstname" => "Philip");

$count = ues_user::count($by_philip);

if ($count == 1) {
  $philip = ues_user::get($by_philip);
} else {
  $philip = ues_user::get($by_philip + array("lastname" => "Cali"));
}

$philip_two = $DB->get_record("user", $by_philip);

ues_user::upgrade($philip_two)->save();

$by_smith = array("lastname" => "Smith");

foreach (ues_user::get_all($by_smith) as $smith) {
  echo "$smith->firstname $smith->lastname<br/>";
  $smith->delete($smith->id);
}

// I think you get the idea
Clone this wiki locally