Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to separate data access layer in codeigniter

I used to write the data access functionalities in model itself. Now I want to separate data access from business logic. I am using codeigniter as framework. It seems that one way of doing it is use ORM, but it will have a performance penalty I guess.

are there any general best practices?

like image 357
Myth Avatar asked Feb 11 '26 20:02

Myth


1 Answers

Have a look at POEAA's Data Source Architectural Patterns:

  • Table Data Gateway
  • Row Data Gateway
  • Active Record
  • Data Mapper

CodeIgniter claims to use ActiveRecord, but it doesnt. It's more like a rudimentary QueryObject. To truly separate your DAO from your Domain objects, you have to use a DataMapper. Depending on the complexity of your mapping needs you can build one yourself or use an ORM. Ironicaly, the majority of ORMs in the PHP World are based on ActiveRecord, which is pretty ill-suited for ORM. Doctrine 2 is the only I know that uses a DataMapper approach.

An ORM will always come with a performance penalty (and it can be a serious one). However, you should not rule out an ORM just because of that. Handcrafting an efficient DataMapper in a high impedance mismatch scenario can be tedious and difficult work. Again, see POEAA for a list of common Object-Relational patterns.

There seems to a DataMapper implementation for CodeIgniter with Overzealous DMZ. I have never worked with it and cannot say anything about it. It just came up after a quick google, so I thought I add it here.

like image 109
Gordon Avatar answered Feb 13 '26 08:02

Gordon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!