ABSLife
ABSLife

Reputation: 45

Simple ORM (without external library)

Long story short, a group project I am involved in is not allowed to use external libraries for the project.

We wrote a custom framework due to this, but we are stalled at a point. We need a simple ORM to handle relationships (again, no external libs aloud such as Doctrine, Propel, etc), and how to do it, easily.

For example if we had a model with...

class ProductsModel extends ModelLib {
    $has_many = array( 'Images' ); // Would relate to Images, get all images with this product_
    $has_one = array( 'User' ); // Would relate to the User, get 1 user who uploaded this product.

Can someone help us on a path to accomplish this, so we can call:

$products = $productsFinder->findAll( ); // returns all product models
foreach( $products AS $product ) {
    print $product->user->name; // gets the user who uploaded the product by relation
    print_r( $prodoct->images ); // returns models of images related to this product.

This is just an example. We pretty much just want to be able to relate the data easily but we don't know of how to do this (code-wise, or even where we should put such code (ie in the finder or model or?). Can anyone please assist :)

Upvotes: 2

Views: 827

Answers (3)

mario
mario

Reputation: 145482

Two simple ORMs are Idiorm and Axiom. Maybe you can take a little inspiration there. But see also previous discussion here https://stackoverflow.com/questions/185358/simple-php-orm

Upvotes: 3

lotsoffreetime
lotsoffreetime

Reputation: 1110

You basically need to implement all the Data Mapper, Active Record and other patterns connected with them. Martin Fowler's excellent "Patterns of Enterprise Application Architecture" would be a good start. Sorry for being terse, but it's a big subject!

Upvotes: 1

Joe Mastey
Joe Mastey

Reputation: 27119

Take a look at how your favorite ORM library was coded, so that you understand how they decided to structure the system. Now write your own version of that.

There are many nuances to an ORM system -- and many design decisions -- and it doesn't seem likely that anyone will be able to post the code for one here.

Upvotes: 0

Related Questions