Check inside a model's method if it has been called from the view

50 views
Skip to first unread message

Zop Zoup

unread,
Nov 12, 2012, 12:52:13 PM11/12/12
to joomla-de...@googlegroups.com
Hi, 

I'm still creating a component. In the goal to populate an edit form, I'm adding informations in my item list in the view. Exemple to populate in  a form a field named testField wich is not present in the foo table:
(I detail here beacause it's quiet hard to find those informations, and this group have a good visibility in google)

my_component/views/foo/view.html 
...
function display($tpl = null) 
{
   // get the Data
   $form = $this->get('Form');
   $item = $this->get('Item');
  ...
   // Assign the Data  
  $this->form = $form;
  $this->item = $item;
  $this->item->testField = $this->get('valueForTestField');
}

The default Joomla behaviour is that $this->get('valueForTestField') call a method in the foo Model named 'getValueForTestField'. 
my_component/models/foo.php 
...
function getValueForTestField(&$pk="")
{
   // Return the desired value
}

First Question : Is it possible to parse an argument ($pk) to this model's methods from the view ? Something like  $this->get('valueForTestField', array_of_arguments) ?

Because I didn't known how to parse an argument to this function from the model, I've included a process to find the needed informations from the foo datas :

my_component/models/foo.php 
...
function getValueForTestField(&$pk="")
{
   if($pk="")
   {
      $data = $this->getItem(); // Get the infos from the  'foo table' (Joomla Core)
      $pk = $data->id;
   }
  ...
}

The goal of if($pk) is to check if the method have been called without arguments, so if it have been called from the view.

Second Question : Is there a more correct way to check in a model's method if it has been called from a view ? 

Thanks a lot. 



Donald Gilbert

unread,
Nov 12, 2012, 1:03:34 PM11/12/12
to joomla-de...@googlegroups.com
I've followed this same paradigm before, so I know what the use case is, and I say that it's perfectly fine the way you are doing it. I don't know if there's a "correct" way, but I'm reassured now that I know another developer came up with the same method that I did. :)

As far as calling $var = $model->get('Method', array()), that's not possible. You CAN pass a second variable, but it's just serves as the default value to fetch OR as the model name to use to run the specified 'Method'. If the second argument doesn't match an existing/known model, that argument will be returned as the value for $var.





--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/GwrQATgpGG4J.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

Michael Babker

unread,
Nov 12, 2012, 2:26:36 PM11/12/12
to joomla-de...@googlegroups.com
It's one of the limitations of the legacy view class.  In the new MVC view, since you must instantiate it with a JModel instance, you can directly call a model's methods using $this->model->myMethod($param1).

That being said, the legacy view class has a getModel method, which looks to return a model class instance.  So perhaps you could retrieve your model from the view using that method then call your method with that result, so:

$model = $this->getModel();
$model->myMethod($param1);
Reply all
Reply to author
Forward
0 new messages