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?

 

Views: 178

Replies to This Discussion

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

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.

 

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

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.

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!

Hi Amy, I just installed a clean J1.7 with the latest version of JFooBar and it's working great! Nothing to report! :)

RSS

Badge

Loading…

© 2012   Created by Amy Stephen.

Badges  |  Report an Issue  |  Terms of Service