3

I am trying to call model's method from controller.

controller.php

if($view='myview'){   
      $model= $this->getModel('mymodel');  //on echo-prints class name of model
      $locations= $model->get('Locations');
      var_dump($locations);//prints NULL
      ...
}

mymodel.php

public function getLocations(){  
      $db=JFactory::getDBO();
      $query=$db->getQuery(true);  
      $query->select('distinct location');   
      $query->from('#__locations');
      $db->setQuery($query);  
      return $loc = & $db->loadObjectList();  
}

Why it's returning NULL in controller, however getLocations() working fine in view.html.php.

1
  • Side note: However $db = JFactory::getDBO(); works in your model but it's better to define the db connection like so: $db = $this->getDbo();.
    – Farahmand
    Sep 21, 2014 at 6:38

2 Answers 2

5

You don't do $model->get('Locations'). The correct syntax is to just call the method name: $model->getLocations().

Using the ->get('Locations') syntax is only valid in a view (because get() is a method of the view class).

This doesn't throw an error on the model, because it does also have a get method. However, it does a completely different task than being a reference for other methods.

--

Lots of other things that I want to note about the code:

  1. Your if statement in the control should have two = signs. Otherwise you are setting the view to be that string (so it will always run).

  2. Why do this: return $loc = & $db->loadObjectList();? Storing the object list in a local variable at the end is just odd. And you definitely don't need to store by reference by using the &. Just do return $db->loadObjectList();

  3. This one may not be true in your case, but if the model and the controller are from the same set (i.e. have the same name) you can usually get away with just doing $model = $this->getModel(); instead of $model= $this->getModel('mymodel'); The controller already defaults to the model of the same name.

1
  • Nice one with the get. I missed that. :) Sep 20, 2014 at 19:29
3

There is very little background on what else is in the component.

You really need to check for database exceptions. See: How to do SQL exception / error handling compatible with 2.5 and 3.x?

I see two options here:

1. Recommended

You set up your debugger in your prefered IDE and look into the code to see what it does and where is the null coming from.

This will help you not only now, but also on the long run.

2. May work

Disadvantage of this method is that your code quality will continue to be poor and the next null you get will result into a new question asking for help.

Since it seems that $model is an instance of your model, the only chance I see if somehow getLocations() not being called or loadObjectList() returning a null as a result of a failed query. Not excluded is also a typo somewhere.

1
  • My first debugging step would have been an exit or var_dump in the model's getLocations method. That would likely show that the model never gets called. Sep 20, 2014 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.