Similar models in both backend and frontend ?

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
duddy67
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 229
Joined: Sun Jul 12, 2009 12:04 pm

Similar models in both backend and frontend ?

Post by duddy67 » Thu Aug 18, 2016 6:02 am

Hi

In my component I have 2 similar models,
one in the backend:

Code: Select all

class MycomponentModelOrder extends JModelAdmin
{
  ...

  public function someSpecificFunction($data)
  {
    ...
  }
}
and the other one in the frontend:

Code: Select all

class MycomponentModelOrder extends JModelAdmin
{
  ...
}
How can I call someSpecificFunction nested in the backend model from the frontend model (or from its view) without generate conflict ?

Thanks

over
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Thu Jan 18, 2007 5:35 pm

Re: Similar models in both backend and frontend ?

Post by over » Thu Aug 18, 2016 9:58 pm

Have a look in e.g. the frontend model com_content, form.php

Code: Select all

// Base this model on the backend version.
require_once JPATH_ADMINISTRATOR . '/components/com_content/models/article.php';

/**
 * Content Component Article Model
 *
 * @since  1.5
 */
class ContentModelForm extends ContentModelArticle
{

Hope that gives you some hints. Alternatively it should be ok to instantiate the backend model.

duddy67
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 229
Joined: Sun Jul 12, 2009 12:04 pm

Re: Similar models in both backend and frontend ?

Post by duddy67 » Fri Aug 19, 2016 5:57 am

I've already check this one.
To get it working I have to use a different name from the extended class name, but I need the same name for the frontend model.
I tried to instantiate the backend model in the frontend view but it didn't work.

Would you have some example ?

over
Joomla! Intern
Joomla! Intern
Posts: 83
Joined: Thu Jan 18, 2007 5:35 pm

Re: Similar models in both backend and frontend ?

Post by over » Fri Aug 19, 2016 7:23 am

But you can generally change the code?
I can't think of a way to load the same classname twice but I'm not THE expert.

One way could be to add a new classlevel, move the common code to that one and extend both front- and backend from that new class.

Code: Select all

class MycomponentModelOrdernew extends JModelAdmin
and for the old classes

Code: Select all

require_once JPATH_ADMINISTRATOR . '/components/com_mycomponent/models/ordernew.php';

class MycomponentModelOrder extends MycomponentModelOrdernew 


Locked

Return to “Joomla! 3.x Coding”