understanding mvc: difference between a library class and a model class
so i want to follow the mvc conventions but haven't really got the hang of it.
i've got problem understanding the explicit difference between a model and a library class and how they relate to each other.
eg. i want to create classes to add/edit contacts and also adding them to a group.
i thought it should be divided into 2 classes: Contact and Group.
The first class would create a contact. The other one will add the contact to a group. I thought this dividing would be perfect cause their logic is isolated from each others. Contact doesn't care if Group exists or not. Group doesn't care about how a Contact looks like.
So Contact will have these methods:
$Contact->add($name, $email, $address, $phone) // create an entry in database
$Contact->delete($id) // delete the en开发者_开发百科try in database
$Contact->edit($id, $name, $email, $address, $phone) // edit the entry in database
And Group:
$Group->createGroup($name) // create a group in database
$Group->delete($id) // delete a group in database
$Group->addContact($groupId, $contactId) // add a contact to a group in database
So these classes apparently work with the database. Does this mean that these are models? Or are they library classes that eg. should be put in SYSTEM/LIBRARIES in CodeIgniter. If it's the latter one, how do a Model in this case looks like using the classes?
And how would a controller look like in this scenario?
Would be great if someone could give me the big picture! Thanks!
The MVC pattern does not have the concept of a Library. What you are refering to is a general usage pattern of MVC frameworks. That is having a model folder, which contains application specific classes often derived from generic, reusable and application independent library classes.
Generally Models are like objects, they interact with the database, have variables and functions, and are reusable.
Libraries are normally sets of helper functions to process data / perform actions.
精彩评论