[FIXED] Passing variable to model from view

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
adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 94
Joined: Thu Sep 15, 2005 7:30 am

[FIXED] Passing variable to model from view

Post by adam.hughes » Fri Feb 05, 2016 4:13 pm

I'm trying to improve my component by switching the way that I use models to what I believe is the correct way.
I'm switching my component to use the setModel in the controller to set the models up for the view and then get teh view to call the models.
I can set up multiple models and call my basic functions fine, but I cannot find out how to call a funciton inside the model that requires a parameter being sent to it.
My code is as follows :

Code: Select all

Model (the function)
function getbp($cardcode)
{
.....
}

Controller (setting the model)
$view->setModel($this->getModel('bp','questproductModel'));

View (calling the function inside the model)
$result = $this->get('bp','bp');
The code runs fine, but I cannot find a way to pass my variable (cardcode) over to the model from the get statement in the view.

Cheers
Last edited by adam.hughes on Fri Feb 05, 2016 6:35 pm, edited 1 time in total.

deleted user

Re: Passing variable to model from view

Post by deleted user » Fri Feb 05, 2016 5:09 pm

The get method only takes one parameter, a method to call (whose method name is prefixed with get). If you want to pass parameters from the view, use the getModel method in the view. So this should work:

Code: Select all

$result = $this->getModel('bp')->getbp($cardcode);

adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 94
Joined: Thu Sep 15, 2005 7:30 am

Re: Passing variable to model from view

Post by adam.hughes » Fri Feb 05, 2016 6:01 pm

Thanks for the reply, your code works perfectly except that I was under the impression that the preferred method of setting the models for a view was from the controller and not inside the view itself ?

Is there anyway of setting the model in the controller and then calling the function from the view (and obviously passing the parameter over) ?

adam.hughes
Joomla! Intern
Joomla! Intern
Posts: 94
Joined: Thu Sep 15, 2005 7:30 am

Re: [FIXED] Passing variable to model from view

Post by adam.hughes » Fri Feb 05, 2016 6:37 pm

I have found a couple of other examples and all of which seem to indicate that you have to use the getModel from the view if you are passing a parameter and this is the required behaviour.

You still need to do the setModel in the controller, the get method in the view is basically a shortcut for the getModel call and can be used if you do not pass a parameter.

Cheers for the fix


Locked

Return to “Joomla! 3.x Coding”