dev-master
9999999-dev https://github.com/jamend/selective-ormSelective Object-relational mapper
MIT
The Requires
- php >=5.4.0
The Development Requires
by Jonathan Amend
orm database selective
Selective Object-relational mapper
Selective ORM is a database abstraction layer that simplifies working with databases. It is fully object-oriented, and infers the database schema (tables/columns/relationships) automatically, so you don't have to repeat it in your code., (*1)
If you'd like to try Selective pending a stable release, you can add this to your composer.json:, (*2)
{ "require": { "jamend/selective-orm": "dev-master" } }
Connecting to a database, (*3)
// first argument is the database name // second argument is the driver implementation class name // third argument is the parameter array for the driver $db = new \selective\ORM\Database( 'sample', 'MySQL', // driver class ['host' => 'localhost', 'username' => '...', 'password' => '...'] // MySQL driver parameters );
Getting a record from the database, (*4)
$books = $db->Books; // get a Table instance for the Books table in the database $book = $books->{12}; // get a Record instance for the book with ID 12 echo $book->title; // columns map directly to properties of the record
Looping through records with a where and order by clause, (*5)
$someBooks = $db->Books ->where('tite LIKE ?', 'The%') ->orderBy('datePublished', 'DESC') ; // fluent interface // $books will lazy-load the records once you start iterating through them foreach ($someBooks as $id => $book) { echo "#{$id} => {$book->title} <br />"; }
Unbuffered record set, (*6)
By default, all rows matching the query are fetched when starting to iterate through them. This can be a performance problem when dealing with a very large number of records, since they are all kept in memory and PHP cannot garbage-collect them. As an alternative, an Unbuffered record set can be used, which allows you to iterate through the results like a stream of records:, (*7)
$bookStream = $books-> ->orderBy('datePublished', 'DESC') ->unbuffered() // switch to unbuffered ; foreach ($bookStream as $book) { // ... }
Persisting changes, (*8)
Create book, (*9)
$newBook = $books->create(); $newBook->title = 'A New Book'; $newBook->datePublished = time(); $newBook->save(); // $newBook's id is automatically set to the auto-increment ID echo "New book created with ID {$newBook->getID()}";
Update book, (*10)
$book = $books->{13}; $book->title = 'A Better Title'; $book->save();
Delete book, (*11)
$books->{14}->delete();
Selective can also simplify the use of relationships (foreign key constraint) that are defined in the schema., (*12)
Each relationship will be mapped to the table's columns by the column name. If a column has a relationship, the resulting record properties will represent the record at the other end of the relationship., (*13)
Here are some examples:, (*14)
Get the author of a book, (*15)
$book = $books->{15}; $author = $book->idAuthor; // $author will be a Record for the author matching the book's idAuthor echo $author->name;
Get all books by an author, (*16)
$authors = $db->Authors; $author = $authors->{1}; $books = $author->Books; // $books will be the Books table filtered by the author
Set the author of a book, (*17)
$author = $authors->{2} $book = $books->{16}; $book->idAuthor = $author; // '2' would also work $book->save();
Relationship optimization, (*18)
Related records are by default lazy loaded, meaning that the author for $book->idAuthor or the book record set for $author->Books will not be loaded until you request them. This is undesirable when working with a record set and its related records in batches, as it will result in many queries to the database being called in a loop. To demonstrate:, (*19)
foreach ($db->Books as $book) { // to get every book's author's name, a query must be sent to the database to fetch the book's author echo $book->idAuthor->name; }
To avoid this, the RecordSet::with($tableName) method can be used to tell Selective to pre-load the related records for a RecordSet:, (*20)
foreach ($db->Books->with('Authors') as $book) { // the author for each book will already be pre-loaded using the same query that fetched the books echo $book->idAuthor->name; }
Selective Object-relational mapper
MIT
orm database selective