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.31k
    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

    comp1->model->m1
      model: m1

    comp1->controller->c1
      controller: c1
    >> comp2

    comp1->model->m2
      model: m2

    comp1->controller->c2
      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:

    1.$type
    The model type to instantiate. (m1)

    2.$prefix
    Prefix for the model class name.(Optional) (Comp1Model)

    3.$config
    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: