![]() |
Examples of using the Model class? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Examples of using the Model class? (/showthread.php?tid=9759) |
Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Doug Lerner[/eluser] Hi, I am a newbie to CI. Are there any examples of small apps that make use of the Model class and active records to create a database, create some tables, add/modify/delete records and some basic things like that? I think if I can see one basic example that puts it all together it would make things a lot clearer. Thanks! doug Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Colin Williams[/eluser] [quote author="Doug Lerner" date="1215518910"]I think if I can see one basic example that puts it all together it would make things a lot clearer.[/quote] Here's a good example. This Model method handles the saving of an object (whether it is a new object or a pre-existing one), in this case, a User. A seemingly simple operation easily comes quite complex. However, by keeping this in a model, you make your edit user and create user controller methods much more simple, leaving room for the complexity of validation, etc. Code: function save_user($user) Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Doug Lerner[/eluser] Thanks. I will take a look through that code for sure. And if there are any other complete examples out there - like a tutorial maybe that says (1) create this file in the models folder, (2) this in the controllers and (3) this in the views and here we tie them all together, it would be useful. I would be happy to contribute such a tutorial myself if I ever figure it all out. ![]() doug Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Colin Williams[/eluser] K.. quick tut. Very very simple. Here's the run down. The Blog Controller is called because the user enters our blog URL. We load the blog model to handle retrieving our data. We use the model to retrieve the data and pass it into the view. Our view loops through the result from the model and displays it in a manner we want. Excuse the brevity and shit HTML ![]() Controller in controllers/blog.php Code: class Blog extends Controller { Model in models/blog_model.php Code: class Blog_model extends Model { View in views/blog/front.php Code: <html> Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Doug Lerner[/eluser] Thanks. That was useful. One question - how did the tables in the database get created to begin with? Is there something equivalent to RoR "migration" that takes the model and creates tables with the appropriate fields and all in CI? Thanks, doug Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Colin Williams[/eluser] Either you executed MySQL commands from the command line to create your database and table, or you used a web-based MySQL client like phpMyAdmin to set up the database and tables, or you used a desktop client like Navicat (Windows). I typically use phpMyAdmin Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Doug Lerner[/eluser] Or I supposed you could also execute MySQL requests from inside some initialization function in the model class to do it, couldn't you? Or in real-time check for the existence of a table before using it and creating it if it doesn't exist? Or are there active record functions which do that? Isn't it better to automate things rather than stop and create tables manually? Thanks, doug Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Colin Williams[/eluser] Quote:Isn’t it better to automate things rather than stop and create tables manually? Not a bad idea if you're going to distribute the App or reuse it a lot. Otherwise, I plan my database schema first, build it out, then start coding. You could also do it dynamically in something like an install script. But if you're just creating a bespoke App with a narrow scope, I don't think that complexity is necessary Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Doug Lerner[/eluser] Correct me if I'm wrong... I'm admittedly a super-newbie at CI.... I am getting the impression that since the Model part of MVC is "optional" in CI that what gets put into the models folder are really more or less "database helper functions" which might just as well be in the controllers folder as functions in a class definition there. Is there a practical difference - something with the naming conventions of the framework, that make it useful to have these functions inside the models folder hierarchy? Another thing I'm still not getting from the examples and discussion and docs so far - is there anything in CI per se that helps set up basic CRUD for a database table (in the way scaffolding and database migration does in Ruby on Rails, for example)? It seems that it takes as much time to set up a basic CRUD framework for a database table in CI as it would if doing it outside the framework. But as I said, I am an admitted newbie at CI. Any other thoughts on how the framework assists in setting things up? Thanks! doug Examples of using the Model class? - El Forum - 07-08-2008 [eluser]Colin Williams[/eluser] CodeIgniter's own stated philosophy, and mine as well: Quote: * The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database. And no, beyond scaffolding, there is no automated CRUD-type set up in CI core, but there have been several Libraries contributed to expedite CRUD processes. Although, in my experience, a given application component's CRUD process is too unique to farm out to a CRUD-type framework. |