tags:

views:

117

answers:

2

I have never really appreciated ORM's so I think that the only way to remedy this is to build a basic one myself so I can see what all the hubbub is. So with that in mind, what are the basic features I would need to include to make a semi-usable ORM?

As far as I can tell, it would basically need to work like this for the end programmer:

/*
 * Create a user
 */
$user = new User();

$user->name = 'Joe';
$user->email = '[email protected]';
$user->save();
unset($user);


/*
 * Create a game
 */

$game = new Game();
$game->name = 'soccer';
$game->save();


/*
 * Set all users as players
 */
$users = ORM::factory('users');
$users = $users->findAll();

foreach ( $users as $user ) {
    $user->setGame($game);
    $user->save();
}

unset($users);


/*
 * Get all games and show all users
 */

$games = ORM::factory('games')->findAll();

foreach( $games as $game ) {
    print $game->name;

    print 'Users in game:';
    foreach( $game->users as $user ) {
     print $user->name;
    }
}

Each model class would extend the ORM class which would have all the basic methods

  • find($id)
  • findAll($where)
  • save()

Other usefull features would be things like:

  • Able to request rows with a certain id User::find(34)
  • Able to limit result rows with WHERE like options
  • Able to tie one row object to many rows from another table. (1 to many)
  • Query building so that the SQL was written automatically.

Could anyone else tell me what I would need. I've been looking at some of the libraries like Doctrine, EZPDO, dORM, and KohanaPHP but I can't seem to find a library that is easy to digest to figure out what the feature list would need to be to tackle this project.

:EDIT: I found an image detailing some of ruby's offerings and some more info on the IgnitedRecord Project.

+1  A: 

Please make sure that you can handle many to many relationships.

Scott Saunders
What would an example of this be?
Xeoncross
You might have a DB model where a user can belong to more than one game - so, in the DB you wouldn't have a single FOREIGN KEY on the user, you'd have a join table.ORM should handle this by offering a user->getAllGames() or something similar.Also to think about:1. Offer ability to eager or lazy load associated games when loading a user2. Offer ability to save all associated games when saving a user.
Steve Claridge
+1  A: 

Here is a list of basic and extended features ORM is supposed to have: http://madgeek.com/Articles/ORMapping/EN/mapping.htm

FractalizeR