Experimental ORM layer for Moodle
- Drop korma.php into your project
- Add a require('korma.php') to your script
Unit tests are included. Run them with PHPunit in the usual way.
class Author extends Model {
protected static $table = 'author';
protected static $fields = array(
'firstname' => 'string',
'lastname' => 'string'
);
protected static $one_to_many_relations = array(
'books' => array(
'model' => 'Book',
'field' => 'authorid'
)
);
}
class Book extends Model {
protected static $table = 'book';
protected static $fields = array(
'title' => 'string',
'year' => 'integer'
);
protected static $many_to_one_relations = array(
'author' => array(
'model' => 'Author',
'field' => 'authorid'
)
);
}
You don't have to describe all fields in the database. Just the ones you want to use.
Book::get();
Returns an array of all books in the DB.
Book::get_one();
Returns the first book in the database. If a filter was specified, would return the first matching result. If there are no matches in the database, returns boolean false.
Book::count();
Returns a count of all books in the DB. Can also be used with filters.
Book::get(array(
'year' => 1929
));
Returns a list of books published in 1929.
Can also be written as:
Book::get(array(
'year__eq' => 1929
));
Other filters are available:
- ieq (case insensitive)
- gt
- gte
- lt
- lte
- startswith
- istartswith
- endswith
- iendswith
- contains
- icontains
- in
Book::get(array(
'year__gt' => 2010
'title__contains' => 'PHP'
));
Returns all books that have PHP in the title AND were published since 2010.
Book::get(array(
array(
'title__contains' => 'Javascript'
), array(
'title__contains' => 'jQuery'
)
));
Returns all that have Javascript OR jQuery in the title.
Book::get(array(
'title__startswith' => 'A',
'author__lastname__startswith' => 'A'
));
Returns all books where the title and the author's last name start with A.
$emily = new Author();
$emily->first_name = 'Emily';
$emily->last_name = 'Bronte';
$emily->save();
Creates the book and saves it to the database.
Can also be written as:
$emily = new Author(array(
'first_name' => 'Emily',
'last_name' => 'Bronte'
));
$emily->save();
Alter properties and re-save:
$emily->last_name => 'Brontë';
$emily->save();
Properties can be other objects if many-to-one relationships are defined.
$heights = Book::get(array(
'title' => 'Wuthering Heights'
));
$heights->author = $emily;
$heights->save();
Book::delete(array(
'year__lt' => 1984
));
Deletes all books released prior to 1984.
$emily->get_related('books');
Returns a list of Emily's books.
$emily->add_related('books', array($heights));
Sets $height's authorid to $emily->id.
$emily->remove_related('books', array($heights));
Removes Emily's books by NULLing the book->authorid field. Note: some 'FK' fields in Moodle aren't NULLable (course_completions.course, for example). In these cases, using model::delete may be more suitable.
$emily->set_related('books', array($heights));
Removes Emily's existing books then adds the ones specified.
All of these functions will accept an array of instances or IDs.
$emily->refresh();
Pulls all fields directly from the database and updates the instance.
For queries that are too complex to be expressed with the array structure described above, use the following functions which all accept a raw SQL where clause.
Author::get_raw("lastname = 'Brontë'");
Author::get_one_raw("lastname = 'Brontë'");
Author::count_raw("lastname = 'Brontë'");
Author::delete_raw("lastname = 'Brontë'");
A very basic, Silex-based Moodle local plugin is included in the examples folder.