codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
Domain class --------------------- File: /application/domain/Book.php class Book { public $id; public $title; public $release_date; } Defining ORM Metadata --------------------- File: /application/domain/mapping/Book.php class BookMapper { public $table = 'Book'; public $columns = array( 'title' => array('name'=>'Title', 'type'=>'string') ); } class BookConstraints { public $constraints = array( 'title' => array('maxSize' => 200) ); } class BookRelationships { public $belongsTo = 'Author'; public $hasMany = array( 'authors' => 'Author' ); } Repository --------------------- The Repository pattern offers a mechanism to manage the Book Entity and abstract the persistence away from the Domain object. $book = new Book(); $book->name = 'ZF in Action'; $repository = new BookRepository(); $repository->setEntity($book); $repository->setDao(new BookDao()); $repository->setMapper(new BookMapper()); $repository->save(); // Or... $book = Zf_Orm::new('Book'); // returns an instance of BookRepository $book->setTitle('ZF in Action'); // EntityManager.getEntity('Book')->$property = $value $book->save(); // Or... $book = Zf_Orm::new('Book', array('title'=>'ZF in Action')); $book->save(); Dynamic Finders --------------------- A dynamic finder looks like a method invocation, but the methods themselves don't actually exist in any form at the code level. $book = Zf_Orm::new('Book')->get(1); $books = Zf_Orm::new('Book')->getAll(1, 2, 3); Method expressions can also use a boolean operator to combine two criteria: $book = Zf_Orm::new('Book'); $books = $book->findAllByTitleAndReleaseDate($title, $date); Relationships --------------------- $book = Zf_Orm::new('Book')->findByTitle('ZF in Action'); $book->addToAuthors(array('name'=>'Matthew'); $book->save(); $book = Zf_Orm::new('Book', array('title'=>'ZF in Action')); $author->addToBooks($book); $author->save(); Querying associations --------------------- $author = Zf_Orm::new('Author')->findByName('Stephen King'); $books = Zf_Orm::new('Book')->findAllByAuthor($author);
Private
[
?
]
Run code
Submit