Welcome Guest, Not a member yet? Register   Sign In
ORM
#1

[eluser]Clifford James[/eluser]
Hi all,

I'm building my own ORM, because I don't like how other ORM's handle my objects.

The project can be found on GitHub.

Note: primary keys need the be 'id' and numeric, i'm planning to make it more flexible later.

Tabels used in this example:
customers
-> id (int)
-> name (string)

contacts
-> id (int)
-> customer_id (int)
-> name (string)

Installation:
1. Move the files ORM.php and ORM_validation.php to the CodeIgniter application/libraries folder.
2. Move the orm.php config file to the CodeIgniter application/config config folder.
3. Make sure you have the following settings in your application/config/autoload.php config file:
Code:
$autoload['libraries'] = array('database');
$autoload['config'] = array('orm');

Thats it for the installation.

Models
A model for the ORM looks like this:
Code:
<?php

class Customer extends ORM {
  
  // here you define the table in the database
  function table()
  {
    return 'customers';
  }

  // here you can define what kind of relations the object have (belongs_to, has_one, has_many)
  function has_many()
  {
    return array('contact');
  }

  // here you can define the (optional) validation of the object for saving
  // the array is the same as for the form_validation of CodeIgniter
  function validation()
  {
    return array(
      array(
    'field' => 'name',
    'label' => 'Name',
    'rules' => 'required|unique'
      )
    );
  }

}

Using the ORM
Code:
$customer = new Customer(); // empty customer object
$customer = new Customer(1); // customer object with id 1
$customer = new Customer($this->input->post('customer')); // customer object filled with given post data

$customer->validate(); // validates customer object with validation rules from model
$customer->validate($rules); // validates customer object with given validation rules

$customer->save(); // saves customer, only if validated, insert new one or update existing
$customer->save($this->input->post('customer')); // saves customer with data from post
$customer->save($contact); // makes relation between customer and contact

$customer->delete(); // delete customer
$customer->delete($contact); // deletes relation between customer en contact

//find((array) $where, (array) $options)
$customer = new Customer();
$customer->find(array('name' => 'Clifford James')); // searches for customers with 'Clifford James' as name, returns arrayObject with customer objects
$customer->find(array('name' => 'Clifford James'), array('order_by' => 'id DESC')); // same as above but with options

//all((array) $options)
//same as find but without the $where

//find_one((array) $where, (array) $options)
$customer = new Customer();
$customer->find_one(array('name' => 'Clifford James')); // searches for customers with 'Clifford James' as name, returns customer object
$customer->find_one(array('name' => 'Clifford James'), array('order_by' => 'id DESC')); // same as above but with options

//first((array) $where, (array) $options)
//same as find_one but automatically sorts ASC on id

//last((array) $where, (array) $options)
//same as find_one but automatically sorts DESC on id

//exists()
$customer = new Customer();
$customer->exists(); // returns FALSE

$customer = new Customer(1);
$customer->exists() // returns TRUE if found

/* RELATIONS */
$customer = new customer(1);
$contacts = $customer->contact()->all(); // $contacts contains arrayObject of contacts related to customer
$contacts->first(); // returns first contact object
$contacts->last(); // returns last contact object

//other methods used for relations
$customer->contact()->first(); // first related contact
$customer->contact()->last(); // last related contact
$customer->contact()->all()->delete(); // delete all contacts relations
$customer->delete($customer->contact()->all()); // delete only the relation between customer and contact

//you can even do this
$customer->contact()->first()->customer()->contact->all(); // returns arrayObject with all related contacts

This is just a quick description, more later.

Clifford James
#2

[eluser]WanWizard[/eluser]
Can you explain how this differs from any other ORM out there? I don't see anything new in the examples you give...
#3

[eluser]Clifford James[/eluser]
The difference is that the objects of other ORM's are full of stuff that I doesn't want to be there.

The objects in my ORM contains only the fields that are in the database.
#4

[eluser]Johan André[/eluser]
How about many-to-many and HABTM stuff? Implemented?
#5

[eluser]Clifford James[/eluser]
Customer has one building belongs to customer (1-1)

Customer model:
Code:
function has_one() { return array('building'); }
Building model:
Code:
function belongs_to() { return array('customer'); }

Customer has many servers belongs to customer (1-n)

Customer model:
Code:
function has_many() { return array('server'); }
Server model:
Code:
function belongs_to() { return array('customer'); }

Customer has many clients has many customers (n-n)

Customer model:
Code:
function has_many() { return array('client'); }
Client model:
Code:
function has_many() { return array('customer'); }

n-n relations need a third table, in the above example clients_customers will be used
#6

[eluser]WanWizard[/eluser]
As far as I can see, it's just another model (albeit a standardized one).

The advantage of an ORM is exactly the presence of these objects. So you can do things like
Code:
// get all admins
$user = new User();
$user->where('admin', 1)->get();

// get all their messages
$user->posts->get();

In your case, it's nothing more than an abstraction layer on top of some queries, basically Jamie Rumblow base model.

Don't get me wrong, there is nothing against that. Just don't call it an ORM. It's missing the 'O'...
#7

[eluser]Clifford James[/eluser]
I do not agree, with the ORM I build you can do exactly the same:
Code:
/* your example */
// get all admins
$user = new User();
$user->where('admin', 1)->get();

// get all their messages
$user->posts->get();

/* my ORM */
// get all admins
$user = new User();
$admins = $user->find(array('admin' => 1));

// get all their messages
$messages = $admins->post()->all();

// or chained
$messages = $user->find(array('admin' => 1))->post()->all();
#8

[eluser]WanWizard[/eluser]
Which brings us back to my first question: what's new here?
#9

[eluser]Yorick Peterse[/eluser]
Looking through the code I'd have to say this ORM isn't very organized in terms of code. Setting up relations makes no sense, the use of an class just to load another class also doesn't really make any sense. On top of that there are numerous switch statements used rather than using something along the lines of polymorphing. All of this makes this a very non-flexible ORM. Creating an ORM is much more than retrieving elements using methods and takes time.




Theme © iAndrew 2016 - Forum software by © MyBB