views:

105

answers:

1

Say my application has a "Posts" model, and one of the function is add_post(), it might be something like:

function add_post($data) {

    $this->db->insert('posts',$data);

}

Where $data is an array:

$data = array ('datetime'=>'2010-10-10 01:11:11', 'title'=>'test','body'=>'testing');

Is this best practice? It means if you use that function you need to know the names of the database fields where as my understanding of OOP is that you shouldnt need to know how the method works etc etc

+3  A: 

It looks fine for starters. The basic idea is that the model is only interested in CRUD (create, retrieve, update & delete) of data between your application and your data store (i.e. your DB).

If you want to further abstract your database fields from the model itself, allowing it to create methods based on the fields automatically and so on, you can try an ORM (object-relational mapping) framework. A pretty simple one designed for CodeIgniter is IgnitedRecord.

BoltClock
Absolutely it's fine. OOP doesn't require you to not "know how the methods work", it requires you to partition your data relationships and define their interactions. MVC, on the other hand, asks that you separate data, logic and display code. You are still doing this: your model needs the test title, your controller needs it and your view needs it. However, your view doesn't need to know how the title is stored, and what you have shown above doesn't circumvent that; so I will agree with BoltClock (in summary!).
Kurucu