[ create a new paste ] login | about

Project: fedecarg
Link: http://fedecarg.codepad.org/9iWX4l7e    [ raw code | fork | 2 comments ]

fede - Plain Text, pasted on Jan 2:
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);




Create a new paste based on this one


Comments:
posted by fede on Jan 4
GORM (Groovy)
------------

class Book {
static belongsTo = Author
static hasMany = [authors:Author]
static constraints = {
title(maxSize:200)
}
String title
}

class Author {
static hasMany = [books:Book]
String name
}

01 - Example:

def book = Book.findByTitle('Groovy in Action')
book.addToAuthors(name: 'Dierk Koenig')
book.save()

02 - Example:

def book = new Book(title: 'Misery')
author.addToBooks(book)
author.save()

GORM (PHP)
------------

class Book {
static public $belongsTo = 'Author';
static public $hasMany = array('authors' => 'Author');
static public $constraints = array('title' => array('maxSize' => 200));
static public $dataTypes = array('title' => 'string');
public $title;
}

class Author {
static public $hasMany = array('books' => 'Book');
static public $dataTypes = array('name' => 'string');
public $name;
}

01 - Example:

$book = Orm::new('Book')->findByTitle('ZF in Action');
$book->addToAuthors(array('name'=>'Dierk Koenig');
$book->save();

02 - Example:

$book = Orm::new('Book', array('title'=>'Misery'));
$author->addToBooks($book);
$author->save();


reply
posted by fede on Jan 26
Repositories are not part of the Service Layer, they are part of the Domain Layer. The Repository provides a higher level of data manipulation, and acts as a link between the Domain and Persistence Layers (Entity <- Repository -> DAO). The Repository pattern is a very common pattern and most ORMs use it.

Service classes are not part of the Domain or Persistence Layers and therefore do not provide methods to access data, such as dynamic finders. Dynamic finders are usually found in the Repository.

Some people think that ORM is a pattern, but it’s not. Mapping relationships is just a technique that uses different patterns to solve the problem of converting data between incompatible systems, e.g. DataMapper, DAO, Factory, Repository, Service Locator, etc.
reply