Every time I build a new component I wonder if I should use the admin model on the front end or create a separate model and duplicate most of the code. Since the model already contain all the code to get the data from the database, is there a good reason for a separate model? If I take com_compoent for example, retrieving an article or a list of articles should be the same SQL query regardless if it's called from the back or the front end. Same thing for saving an article, we need to set the modification date, the user id who made the modification etc.
What's your take on that?
Tags:
Permalink Reply by Daniel Dimitrov on September 2, 2011 at 6:40pm Hey Alan,
I think that it is really simple - don't repeat yourself. If you can save 100 lines of code, why write them? If you can use your backend models in frontend, then do it. Look at the nooku framework for example - the idea behind it is very simple - don't repeat yourself - write code that you can use everywhere and in any component...
Cheers,
Daniel
Permalink Reply by Alain Rivest on September 2, 2011 at 7:42pm That's what I think. It also minimize the risk of bug. But most if not all the component I looked at uses 2 separates models. Where I work we never duplicates code. The client applications use the same functions as the batch jobs to get or update data from the database. It's common sense.
Maybe I should write some helper classes that are called from the admin models and from the front end models. I understand that some functions are only used on one part of the extension, but a lot of code can be reused.
Permalink Reply by Eddie May on September 3, 2011 at 12:22am In Ruby DRY (don't repeat yourself) is a mantra - DRY code is easier to maintain & bug fix & therefore cheaper in the long run. Perhaps helper classes are the way to go, that way you can override them if necessary depending upon the circumstances.
Would be great if you could show us the end result!
Best wishes,
Eddie
Permalink Reply by Detlef Volmer on September 3, 2011 at 5:08am The only thing I can say is: why not. I do that and don't see any disadvantages.
Have fun.
Detlef
Definitely do that - the only caveat is that the admin uses filters and the frontend uses parameters - so you have to work that thru a bit.
I put a tool and post up on the Magazine this month - it will generate your component for you - and then you can adapt it - http://magazine.joomla.org/issues/Issue-Sept-2011/item/566-Site-Bui...
That'll save a bunch of time too.
1.6 still has too much redundant code - need to get it moved into core.
Permalink Reply by Alain Rivest on September 5, 2011 at 12:21pm Some models uses the JModelAdmin class. I'm not sure if it's a good idea to use that on the front-end. I'll have to study the new classes of J1.7.
I used NotWebDesign's component creator: http://www.notwebdesign.com/joomla-component-creator/index.php?opti...
It did a good job for the admin side, but there's almost nothing for the front-end, so I use your Foobar component as an example! I saw that you renamed it JFooBar and that now it creates the new component automatically but I had errors and the new component is not working. I'll have to test on a clean install. Maybe I messed up my Joomla installation with all the test I did on it!
Alain - if you try it again and find errors - please let me know. HarryB reported some installation errors so those might be related https://github.com/Niambie/jfoobar/issues/6
Thanks!
Permalink Reply by Alain Rivest on September 10, 2011 at 10:41am © 2012 Created by Amy Stephen.