Cannot access context from presenter

Notice: This thread is very old.
Tomas Jancik
Member | 103
+
0
-

I can not access the context from presenter…
I want to initialize the model and giving it the database service

<?php
public function __construct() {
   $this->model = new UserModel($this->context->database);
}
?>

this returns notice
Trying to get property of non-object

What am I doing wrong? How do I access the context?

I'm using Nette 2.0-beta released 24. 8. 2011

voda
Member | 561
+
0
-

It's too soon. The context is not yet set. see PresenterFactory:createPresenter

And you shouldn't create a model in presenter, put it int he config file instead and than use $this->context->model.

mkoubik
Member | 728
+
0
-

Or use startup() method instead of the constructor – don't forget to call parent::startup().

Jan Tvrdík
Nette guru | 2595
+
0
-

You should never override presenter constructor unless you know what you are doing. In this case I wouldn't even recommend you to use startup. You should register UserModel in config.neon and use $this->context->userModel in presenters (as suggested by voda).

config.neon

common:
	...
	services:
		...
		database:
			...
		userModel:
			class: UserModel
			arguments: [@database]

Last edited by Jan Tvrdík (2011-10-01 17:38)