A simple example
The following is a simple example that shows you how to use EZPDO to persist and retrieve your objects.
You may want to check out the tutorial that showcases more advanced features that EZPDO has after following this simple example.
Mapping
Suppose you have a class Book and you want to store it into database. You only need to insert a few lines of comments.
Annotated code
<?php /** * Class of a book * @orm mysql://dbuser:secret@localhost/ezpdo */ class Book { /** * @orm char(64) */ public $title; /** * @orm char(32) */ public $author; /** * @orm integer */ public $pages; /** * @orm boolean */ public $is_easy_to_read; /** * Your regular code for the class here. * ...... */ } ?>
See the highlighted comment lines starting with @orm? @orm is a custom tag in the comment block (DocBlock in phpDocumentor terms). This simple tag does the ORM magic!
A little explanation follows.
The @orm tag
In the class comment block, we have a line
@orm mysql://dbuser:secret@localhost/ezpdo
The orm tag associates the class Book to a Data Source Name (DSN) of the database. The tag simply means that we want the class to be mapped to a table in the database ezpdo. The name of the table is the same as the name of the class. If you want the table name to be different, you can simply write:
@orm my_book_table mysql://dbuser:secret@localhost/ezpdo
For class variable $title, we use
@orm char(64)
to map the variable to a column, which is a 64-char string column. Again if you want the variable to be mapped to a column name different than the variable name, you can write the comment like this:
@orm my_title_col char(64)
Type mapping
Databases of any breed usually support numerous column types. Experience tells us that, because of this, mapping variables into database columns is not a small task. In a deep contrast, EZPDO lets you use only a small set of data types that only has a very few types, but is considered sufficient for most of our tasks. We bet you know each of them well. Take a look at Supported data types.
Easy enough? Next we show how to make your objects into database.
Persisting
The following code snippets show you how to use EZPDO persistence manager to persist your objects to database without writing a single line of SQL code.
Get the manager
Persistence, in EZPDO, is managed by the persistence manager. So let’s get the manager and let it do the work.
// include ezpdo runtime api include_once('/path/to/ezpdo/ezpdo_runtime.php'); // load config.xml (optional: not needed if it is in currrent dir) epLoadConfig('/path/to/your/config.xml'); // get ezpdo persistence manager $m = epManager::instance();
Create an object
Time to call the manger ($m) to create objects for you.
// create a Book $b = $m->create('Book'); $b->author = "ezpdo4php"; $b->title = "The life of a bug"; $b->pages = 21;
Save object to DB
// persist the object $m->commit($b);
And this is it! Your first object has been persisted into your database.
Note that if you enable auto_flush in your configuration, you don’t need to commit the object explicitly for it to be saved to the database. This is automatically done at the end of your script.
Retrieval
Want to find all books by the author and update their info?
Find by example
You can use an example object to find all books by author ezpdo4php.
// use an example object to find object in db $book2find = $m->create('Book'); $book2find->author = "ezpdo4php"; // find all books by ezpdo4php $books = $m->find($book2find);
You can now also use EZOQL to query objects with more flexibility.
Delete and update
Say we want to delete all books that have more than 500 pages by author ezpdo4php and mark the rest of books is_easy_to_read.
// for each book foreach($books as $book) { if ($book->pages > 500) { // delete the thick book (> pages) $m->delete($book); } else { // otherwise, mark the book thin $book->is_easy_to_read = true; // update it in storage $m->commit($book); } }
Wanna do more?
Start from here.