![]() |
Modelling Questions - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Modelling Questions (/showthread.php?tid=4521) Pages:
1
2
|
Modelling Questions - El Forum - 11-29-2007 [eluser]dreamnid[/eluser] Hi all, We want to rewrite our codebase from scratch using CI. However, I'm trying to figure out how to model our code into something that will work with CI. Here is what I'm trying to do (written in Java): Code: public class Person { Code: Person pers = new pers( 1 ); I figured that in CI, Person would be a model. However, I found out you cannot pass parameters to the constructor of models. Some possible solutions: - Add another function to set the id - Create the Person as its own class, but I would lose access to the database functions - Modify the Model class Is there should be a better way of doing this in CI? Thanks! Modelling Questions - El Forum - 11-29-2007 [eluser]tonanbarbarian[/eluser] I simply create a method in my model called load which you then call to get the record Well actually I create a class that extends the core model and put the method in there so that all of my models can use the code Code: libraries/My_Model.php Then the actual model would be models/person.php Code: class Person extends MY_Model { then to instantiate in your controller you do Code: ... Modelling Questions - El Forum - 12-01-2007 [eluser]Unknown[/eluser] Code: class MY_Model extends CI_Model { Um, does this actually work? Because as far as I can tell there is no CI_Model class. The base model class (at least in CI 1.5.4) is called 'Model' which led me to question whether you could actually extend it with a MY_ version like you can (most) of the the other core libraries. My attempt to do it anyway didn't seem to work: Code: class MY_Model extends Model { didn't work The way the CI Loader library is written appears to also exclude overriding the CI model class with your own. Which is a shame and one wonders why? (why?!) If it is possible to do this after all I'd love to hear some more details about how. Otherwise I guess just have to extend the loader library first... Modelling Questions - El Forum - 12-01-2007 [eluser]maadmac[/eluser] [quote author="blueskiwi" date="1196547590"] Um, does this actually work?[/quote] In theory, sure. From the User Guide: Quote:The Database classes can not be extended or replaced with your own classes, nor can the main Controller class. All other classes are able to be replaced/extended. Modelling Questions - El Forum - 12-02-2007 [eluser]tonanbarbarian[/eluser] I have had some instances where the base Model was not extended correctly, and I have had to autoload the Model and the extended Model class. I think the issue is to do with paths because once I had my paths set correctly in my index.php I have not had a problem. Modelling Questions - El Forum - 12-02-2007 [eluser]wiredesignz[/eluser] I usually create a model based on the table with it's related database methods and then create a library which extends the model for each of the row objects. Modelling Questions - El Forum - 12-03-2007 [eluser]CRHayes[/eluser] There are many different opinions about what constitutes a "Model", a "Controller", and a "View". View is really simple...but there is a gray area with Model and Controller. After asking the question multiple times myself, I finally decided the method I would stick with. You are creating an instance of the model (an object) everytime you load it. Therefore, you're model is an object. With that in mind, it should contain the functions relative to that object. Ex: I am building a system that has a user membership system. One of my models is "User". It contains the functions for checking if a username or email address is already registered, inserting new member records, checking if entered Password/confirm password fields (during registration) equal each other, checking if user sessions exist, and getting profile information from the database. Coinciding with what wiredesignz said, this model represents my 'member_profile' database table. Whether this is how someone else does it or not, after much delibertion I think this is the way to do it. The functions contained in your model should relate to the object instance of that model. Modelling Questions - El Forum - 12-04-2007 [eluser]tonanbarbarian[/eluser] After looking more at the loader class code I can see what the issue is with extending the Model class. The model method which is used to load a model is not designed in such a way to allow the Model class to be extended correctly To fix this you can do 1 of 2 things 1. Hack the system/libraries/Loader.php change line 159 Code: require_once(BASEPATH.'libraries/Model'.EXT); Code: load_class('Model', false); or create an extension to the loader to do it for you application/libraries/MY_Loader.php Code: <?php I will be reporting this as a bug if it hasnt been already Modelling Questions - El Forum - 12-04-2007 [eluser]dreamnid[/eluser] Hi again, Thanks for your replies. The way I want to model the Person is you instantiate a person with a specific id. Then you can get attributes of the Person for that specific person. For example: Code: $person = new Person( 32 ); From what I'm read so far, it seems that CodeIgnitor wants the id as a parameter to the member function, such as Code: $this->load->model('Person'); To me, this requires each get function to make a different database call for each parameter that we're retrieving which adds overhead. Am I understanding this correctly? For now, I've being creating the Person class as a library which seems to work okay, but I'm sure it is not ideal practice. I'm trying to find other people's models to see what is supposed to be in a model and how they avoid querying the database for each parameter. Thanks! Modelling Questions - El Forum - 12-04-2007 [eluser]tonanbarbarian[/eluser] Ok there is a minor issue in the CI way of doing things that you put up after you load the model you reference it by the model name Code: $this->load->model('Person'); Ok for what you want to do why not do something like this... just one extra line really Code: $this->load->model('Person'); However if your getId method was to retrieve the data for the record with the specified id then my original way is basically the same |