CodeIgniter Forums
Can a model be more than just a database layer? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Can a model be more than just a database layer? (/thread-15014.html)



Can a model be more than just a database layer? - El Forum - 01-23-2009

[eluser]juddmuir[/eluser]
I've hit a problem using models and attempting to update the database.

If your model is more than just a representation of a database table, then you may have variables in your model that do not exist in the database. So when you come to e.g. update the database using

function update_entry()
{
$this->db->update('entries', $this, array('id' => $this->id));
}

an error is generated if you have a variable that doesn't map to a field. Is the solution to generate TWO objects: one that is the model that you're using, and the other that is the data-model to pass to the db->update function?


Can a model be more than just a database layer? - El Forum - 01-23-2009

[eluser]GSV Sleeper Service[/eluser]
if you want to use an object with db->update, then yes, you will have to make a separate object. from what I understand, codeigniter models are intended to be used as a place to keep your i/o stuff, you're trying to use them almost like an ORM.

you might want to take a look at Doctrine - http://www.doctrine-project.org/ , which can automatically create objects that match your DB.