podataka u objekte • Postoji više ORM patterna: ◦ Active Record (Doctrine 1, Propel, RoR, ...) ◦ Table Gateway (Zend Framework) ◦ Data Mapper (Doctrine 2, Hibernate, ...)
wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. • Data Mapper: A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself. Martin Fowler: Patterns of Enterprise Application Architecture (2003)
biti public • Inicijalizacija kolekcija • Doctrine ne poziva konstruktor (proxy) • Odabir owning i reverse strane relacije! ◦ Owning "drži" relaciju ◦ Owning strana se provjerava prilikom spremanja ◦ Lazy loading
1234); 3 $article->setHeadline('Novi naslov!'); 4 $entityManager->flush(); 5 6 // Lazy load 7 8 // accessing a method of the user instance triggers the lazy-load 9 echo "Author: " . $article->getAuthor()->getName() . "\n"; 10 11 // Lazy Loading Proxies pass instanceof tests: 12 if ($article->getAuthor() instanceof User) { 13 // a User Proxy is a generated "UserProxy" class 14 } 15 16 // accessing the comments as an iterator triggers the lazy-load 17 // retrieving ALL the comments of this article from the database 18 // using a single SELECT statement 19 foreach ($article->getComments() AS $comment) { 20 echo $comment->getText() . "\n\n"; 21 } 22 23 // Brisanje 24 $entityManager->remove($article); 25 $entityManager->flush();
3 ->find($id); 4 5 // All users that are 20 years old 6 $users = $em->getRepository('MyProject\Domain\User') 7 ->findBy(array('age' => 20)); 8 9 // All users that are 20 years old and have a surname of 'Miller' 10 $users = $em->getRepository('MyProject\Domain\User') 11 ->findBy(array('age' => 20, 'surname' => 'Miller')); 12 13 // A single user by its nickname 14 $user = $em->getRepository('MyProject\Domain\User') 15 ->findOneBy(array('nickname' => 'romanb')); • Moguće extendanje Repozitorija: ◦ User::getAllAdmins() ◦ Article::getArticlesByAuthor($author) ◦ ...
select($select = null); 4 public function delete($delete = null, $alias = null); 5 public function update($update = null, $alias = null); 6 public function set($key, $value); 7 public function from($from, $alias = null); 8 public function innerJoin($join, $alias = null, $conditionType = null, $condition = null); 9 public function leftJoin($join, $alias = null, $conditionType = null, $condition = null); 10 public function where($where); 11 public function andWhere($where); 12 public function orWhere($where); 13 public function groupBy($groupBy); 14 public function addGroupBy($groupBy); 15 public function having($having); 16 public function andHaving($having); 17 public function orHaving($having); 18 public function orderBy($sort, $order = null); 19 public function addOrderBy($sort, $order = null); // Default $order = 'ASC' 20 }