Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to call the model from one component to another component in Joomla?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.34k
    Comment on it

    Hello Readers,

    Below blog shows the call of the model from one component to another component.


    For Example - We have a two different components in Joomla :

    >> comp1

      model: m1

      controller: c1
    >> comp2

      model: m2

      controller: c2

    In the above component, call the one component model to another component model.


    Then, we use the below code inside that component model m2 where we call the component model m1:

    Code Example :

    JModelLegacy::addIncludePath(JPATH_SITE . '/components/comp1/models', 'Comp1Model');
    $model = JModelLegacy::getInstance('m1', 'Comp1Model');
    $result = $model->name of the model function();
    print_r($result); die;

    In the above code,

    Step 1 : We use the addIncludePath() method and adds a directory(JPATH_SITE . '/components/comp1/models', 'Comp1Model') means the path of the component model.

    Parameter's are :

    1. the path of the directory.
    2. class prefix for models.


    Step 2 : Then we use the getInstance() method. In this method parameters are:

    The model type to instantiate. (m1)

    Prefix for the model class name.(Optional) (Comp1Model)

    Configuration array for model. (Optional).


    Step 3 : Lastly, we use the name of the model function ($model->name of the model function()) which we used inside the component.


    Advantage of using one component model to another component :
    1. Reusability of a certain function.(suppose if we reuse the certain function that belongs in a model outside of your current scope).
    2. Helps to save time and reduce the duplicacy of the functions in both the components.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: