-1

I am trying to call getServiceLocator in model method but it is throwing below error Fatal error: Call to a member function get() on a non-object

Actually, I am using $this->getServiceLocator()->get('Config'); in model which result the above fatal error

Manik Thakur
  • 294
  • 2
  • 15
  • The error means you're trying to execute a function on not-an-object. You also must show code for us to be able to say anything more. – rkeet Aug 22 '18 at 18:28

2 Answers2

1

You should not use the ServiceLocator in a model class. Refer to this answer: https://stackoverflow.com/a/32858171/1644937, or just search in google for: "service locator antipattern" to get tons of arguments against this practice.

leoap
  • 1,684
  • 3
  • 24
  • 32
0

You coun't not use directly the ServiceLocator in a model class but if you want to use $this->getServiceLocator()->get('Config'); on model then

$config = $this->getServiceLocator()->get('Config'); Assign in controller and pass the $config in madel

$ModelTable->searchList($data, $config);

now you call/use any method/function of config.

susheel sahoo
  • 107
  • 2
  • 8
  • I know I can do this way but I don't want to follow this approach. Looking something inbuilt to use getServiceLocator directly into model. – Manik Thakur Dec 12 '18 at 14:32