CodeIgniter Forums
forms in my model? - 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: forms in my model? (/thread-14449.html)



forms in my model? - El Forum - 01-02-2009

[eluser]ixxalnxxi[/eluser]
Hello All,

In the site I'm developing there is a comment feature. The comment feature will be used over again for different things such as media, articles, blogs, news, etc. I decided to make only one table for comments and then set table and field_id columns so that comments could be inserted for content in different tables.

Now to the question, I am going to be displaying a form for submitting comments and I would rather not rewrite the code for every time I need it (the benefits of OOP, right?). My plan was to put the form display function in my comments model because anytime the model is loaded it would be necessary to have the form too. Is this a bad idea? I fear I might be blurring views and models.


forms in my model? - El Forum - 01-02-2009

[eluser]NogDog[/eluser]
(Caveat: I'm still pretty new to CI.)

An object should focus on one thing. Putting form display into a model object probably does not qualify as such a focus.

I would therfore first consider that it would be its own view if that would work with your design; but if you need to be able to call it from within a view, then I might consider just making it a CI helper function.


forms in my model? - El Forum - 01-02-2009

[eluser]ixxalnxxi[/eluser]
To make it into a CI helper function, I make a Helper class in the helper directory and just add the function there, correct?


forms in my model? - El Forum - 01-02-2009

[eluser]NogDog[/eluser]
[quote author="ixxalnxxi" date="1230977995"]To make it into a CI helper function, I make a Helper class in the helper directory and just add the function there, correct?[/quote]
Yes, or alternatively you could extend an existing helper. For instance, if you're already using the HTML helper, you could just create a "MY_html_helper.php" file in your system/application/helpers directory and define the function there. Then when you do your "$this->load->helper('html');", it will include your new function with it.

It may not be a pure OOP solution, but it would seem pretty clean, simple, and efficient to me.