passing url parameter to the model?

1,836 views
Skip to first unread message

Mike

unread,
May 2, 2012, 5:59:04 PM5/2/12
to joomla-de...@googlegroups.com
How can I access a url parameter in the model?

I've added a parameter to the URL I'm calling from a list view:

index.php?option=com_mycomponent&customer=123456

In the controller's __construct(), I included:

$this->customer = JRequest::getCmd('customer');

What else do I need to do to access this parameter within the model?  In the model, I tried:

$customer = $this->customer;  // is a NULL value

But get a NULL value.

Thanks for the hand,

Mike

piotr_cz

unread,
May 2, 2012, 7:14:53 PM5/2/12
to Joomla! General Development

Controller and Models are not linked like this, both are quite
independent objects. Controller operates model(s), which do it's magic
and return values.

There are few options.

1) After initializing the model in controller, use model state like
this:

$model =& $this->getModel('Customer');
$model->setState('customer', $this->customer);
$customer = $model->getItem();

In the model access it like this:
$customerId = $this->getState('customer');


2) In the model method add a variable
In controller:
$customer = $model->getItem($this->customer);

In model:
public function getItem($customerId)
{
}


3) Usually in Joomla model state (variables you may use in model
methods) is being populated within populateState method but I prefer
setting model state by controller.

Mike

unread,
May 2, 2012, 10:44:07 PM5/2/12
to joomla-de...@googlegroups.com
Thanks!  I tried your first method, and it worked (will experiment with the others later).

One question, what's the point of calling  $customer = $model->getItem();  in the controller?

Best,

Mike

Mike Pearl

unread,
May 3, 2012, 6:57:24 PM5/3/12
to joomla-de...@googlegroups.com
I'm confused by this... this is not working.  Thanks for your patience.

I debugged my code, with your snippet added.  It appears to set the model state from within the controller, and does a getItem().  But I need the Item, based on the value of 'customer' for the view, and do nothing with it in the controller.  So in my componenty, I do the getItem() in the controller then do nothing with it.  When I get to the view, I do another getItem(), but at that point the value for 'customer' isn't available to the view or the model.

I took a look at Joomla's Menu Manager component (com_menus), which passes along a url parameter "menutype".  I can't find anywhere it's referenced until it reaches the items model in populateState().  The line in there reads:

$menuType = JRequest::getVar('menutype', null);

But within my component, $customer = JRequest::getVar('customer', null) returns nothing.  How is the 'menutype' variable make it to the model in com_menus/items?

Mike

Mike

unread,
May 4, 2012, 10:14:25 AM5/4/12
to joomla-de...@googlegroups.com
I've solved this one - in the anchor href I was using dot notation for the task= variable.  At some point in JText::_(), joomla splits this into task= and view= and for some reason stripped my custom variable out of the URL.  I removed the dot notation, in favor of task= and view=.  My custom variable remained in the URL, and JRequest::getVar() now works in the model.

Best,

Mike

Nick Weavers

unread,
May 5, 2012, 7:25:32 AM5/5/12
to joomla-de...@googlegroups.com
When you pass a value to task that has a dot delimiter, Joomla treats the first part (before the dot) as the name of a sub controller (in the controllers directory of your component) and the second part (after the dot) as the method to invoke from that controller. The view to be used with this sub-controller.method combination might be implicit and would be hard coded into the controller, or explicit where you pass it as an additional query string parm as view=myview. I find the sub controllers to be a huge help when organising large and complex components (which form the bulk of our work). 

If you use a value for task that doesn't have a dot in it, the value will be treated as the method to invoke from your main controller.

I'm not sure why the  JRequest::getCmd('customer') within your model mentioned in your first post didn't work. I do that quite often and it always works for me. If you are running under an IDE with a debugger (I use PhpEd which I highly recommend), you could as a "sanity check" insert a break point just before or after the JRequest assignment and  inspect the value of the $_POST and $_GET super global array variables to make sure your customer parm is there, since these are where JRequest gets its values from.

Good luck
Nick 

piotr_cz

unread,
May 7, 2012, 9:08:03 AM5/7/12
to Joomla! General Development
@Nick Weavers:

As I understood it, Mike was requesting variable from within a
Controller (not a Model) and didn't pass it further assuming Model
will have an access to it automatically.

But I have a question for you:
Why are you doing the same in the Model, shouldn't be Controller
responsible for this stuff? The advantage of Controller picking up
request data is that based on those it may decide how to handle it
(ie. which model to use). I'm just curious about your experience /
best practice.

Piotr

Nick Weavers

unread,
May 8, 2012, 8:49:31 AM5/8/12
to joomla-de...@googlegroups.com
@ piotr_cz

Actually I do both, but I must make an embarrassing confession; I never understood the correct way to pass variables from the controller into the model, although I could get them into the view/layout using assignRef. 
I never really figured out the state stuff you mentioned and i managed to dodge my way around it quickly so never came back later to figure it out (lazy I know).
Typically the values I want to pass into the model are those that need to go into a query (after sanitization).

Because I hadn't figured out the correct way to pass variables into my model from the controller, when I needed to do it I would assign the model object to a variable and assignRef it into the layout 
where I could then call whatever method I chose,  passing variables as params in the conventional way (eg $this->mymodel->somemethod($some_parms). 

I also admit that for me it sometimes make sense to push logic down into the layout which may in turn decide to call different methods from the model. I guess I need to own up to being more of a 
hacker than a computer scientist.

Eg.

function showForm() {
// Get the $articleType and articleAlias
$this->getFormField('articleType');
$this->getFormField('articleAlias');

$document = JFactory::getDocument();
$viewType  = $document->getType();
$viewname = "ArticleForm";
# instantiate the view object
$view = $this->getView($viewname, $viewType);
# set the model name
$formArticleModel = 'formArticle';
# instantiate the named model(s)
$formArticleModel = $this->getModel('formArticle');
                # assign the model to the view (as an object variable)
$view->assign('formArticleModel', $formArticleModel);
                # assign the form data to the view (as an array variable)
$view->assign('form_data', $this->form_data);
if (count($this->form_validation_errors)) {
// we were returned here by validateForm()
$view->assign('form_validation_errors', $this->form_validation_errors);
}
$view->setLayout('form', true);
$view->display();
}

Nick
Reply all
Reply to author
Forward
0 new messages