Which ORM use alongside codeigniter

#1
[eluser]Patroklo[/eluser]
I'm thinking of go full ORM into my new codeigniter developments. But I have a lot of doubts, that I think everybody would have in my situation in the moment of choosing a good ORM engine.

I know that adding an orm layer always it's a minus at performance in every language, and also that it will consume more memory than "normal" querys, but I think that if the slowness it's not very high, it would be a great help developing in codeigniter.

I have been playing a little with Doctrine, that seems to be the most famous on PHP right now, and also I have used it in some benchmarking in my own computer and another server, but I have discovered that it's very slow(like 5-10 times slower than normal php or even codeigniter active record using the same object hydrating in the $query->result() function) and also uses a incredibly lot of memory in big querys(in a 8,000 row fetch I got a humoungous 20mb of memory consumption versus the active record object hydrating, in the two querys I finally had the same array of objects, I think that's just ridicolous).

So I'm just wondering which experiences do you have with Doctrine and if you could recommend me another ORM engines, and if, seeing this performances, even makes sense to use them.

#2
[eluser]alexwenzel[/eluser]
I also have had a lot of thoughts about which ORM i should use. I tried Datamapper and Doctrine, but there were always a point where it start to suck.

So I decided to stick with CodeIgniter Active Records and wrote a CRUD Model myself (link in my signature).

Code:
// INSERT data
$user           = new User();
$user->name     = 'new user';
$user->email    = '[email protected]';
$user->password = '123456';
$user->save();

// UPDATE data
$user = User::find('name', 'new user');
$user->password = 'new password';
$user->save();

// REMOVE data
$user = User::find('id', 1);
$user->remove();

This reads like a ORM but is only a CRUD Model-Override. So performance is almost the same like using CodeIgniter Active Records.

Now the interesting Part for you:

You can easily extend the model to add entities or ORM-like behaviour.

Code:
class User extends MY_Model {

  public function getRole() {
    return User_Role::find('id', $this->role_id);
  }

}

class User_Role extends MY_Model {

}

Just as an quick example.

Advantage of this method is:

- You have full controll of the behaviour
- You have the performance of CodeIgniter Active Records

Just my 2 cents on this topic.

#3
[eluser]skunkbad[/eluser]
+1 for using active record and not ORM

#4
[eluser]WanWizard[/eluser]
The biggest mistake a lot of developers make is that when they choose an ORM, they have the idea that each and every problem HAS to be solved using the ORM. And when that fails complain that the ORM sucks, which in fact it's the developers methods that suck.

An ORM is an object relational mapper, which means it maps data collections (in case of ActiveRecord pattern implementations , like Datamapper, always a single record, of in case of a Datamapper pattern implementation it could be something else) to objects.

Due to the fact that instantiating an object is expensive both from a CPU and a memory point of view, you should not use an ORM for "batch" type operations. ORM's however do excel in CRUD operations and relation management, and will make your life a lot easier.

In my talk at this years CICONF about ORM's, I also pointed out that when people talk about ORM's, they swap the model in MVC for an ORM model. And then try to do everything with the standard methods the ORM provides, moving the ORM code to the controller.

Mistake number two. When you select an ORM, it becomes an extra layer between your Model and the datastore. That means you should continue to use Model methods (like for example getUsers() ) in your controller, and in the models getUsers() method use the ORM provided methods to retrieve the data. And if you have an operation that is to complex for the default ORM methods, simply use standard query builder code. Often a lot cleaner and faster.

As always, use the best tool for the job.

#5
[eluser]alexwenzel[/eluser]
Quote:The biggest mistake a lot of developers make is that when they choose an ORM, they have the idea that each and every problem HAS to be solved using the ORM. And when that fails complain that the ORM sucks, which in fact it’s the developers methods that suck.

Dont wanted to offend you or your work Smile

#6
[eluser]toopay[/eluser]
[quote author="skunkbad" date="1350249512"]+1 for using active record and not ORM[/quote]
If you refering to CodeIgniter "active record", its actually a query builder.
If you refering to literal definition of "active record", its actually one of ORM patterns.


#7
[eluser]WanWizard[/eluser]
[quote author="alexwenzel" date="1350296687"]Dont wanted to offend you or your work Smile[/quote]
You didn't.

Just wanted to clarify that like with everything, use the proper tools for the job. Sometimes that an Orm, sometimes it using the query builder, sometimes it's coding SQL by hand...

#8
[eluser]skunkbad[/eluser]
[quote author="toopay" date="1350325769"][quote author="skunkbad" date="1350249512"]+1 for using active record and not ORM[/quote]
If you refering to CodeIgniter "active record", its actually a query builder.
If you refering to literal definition of "active record", its actually one of ORM patterns.

[/quote]

I meant CI's query builder. I'm perfectly comfortable with plain 'ol SQL, so I mostly just like the query builder because it will sanitize my input, and query bindings are super easy. I could see how a person would not be comfortable with complex queries and want more of the query builder features or ORM, but a lot of it just seems like makeup on an old ugly lady. It's not that I want to see her, but I know what I'm dealing with!

#9
[eluser]alexwenzel[/eluser]
[quote author="WanWizard" date="1350326280"][quote author="alexwenzel" date="1350296687"]Dont wanted to offend you or your work Smile[/quote]
You didn't.

Just wanted to clarify that like with everything, use the proper tools for the job. Sometimes that an Orm, sometimes it using the query builder, sometimes it's coding SQL by hand...[/quote]

So true ! Although i really shy from coding SQL. There is always a way to prevent writing pure SQL, in my opinion.

E.g. using views or a different database design, so you can use query builder / active record / ORM again.

#10
[eluser]dejan[/eluser]
[quote author="skunkbad" date="1350355408"]
I meant CI's query builder. I'm perfectly comfortable with plain 'ol SQL, so I mostly just like the query builder because it will sanitize my input, and query bindings are super easy. I could see how a person would not be comfortable with complex queries and want more of the query builder features or ORM, but a lot of it just seems like makeup on an old ugly lady. It's not that I want to see her, but I know what I'm dealing with![/quote]

Besides sanitazion, query builder makes for more readable and maintainable code. Take for example:

Code:
$db->where('x', $a);
if ($condition) {
  $db->where('y', $b);
} else {
  $db->like('z', $c ? 'yes' : 'no');
}
$db->get($table);

In my humble opinion, it looks much better than it would if you were writing it out as a string.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.