Service ‘Model’ not found

Notice: This thread is very old.
thcom
Backer | 72
+
0
-

sorry, i use Nette ony sometimes
so i have big problem, some changes are made in system and my knowledge are on begin again :(

i try to search on forum and google but nothing found

i try to use model i my project

in my old project it was working

i try to use modelX class

config

services:
	- App\Model\UserManager
	- App\Model\ModelX

in /app/model i have ModelX.php file

begin of code :

<?php


namespace App\Model;

use Nette;


/**
 * data model
 */

class ModelX extends Nette\Object	{

	/** @var Nette\Database\Context */
	private $database;

	public function __construct(Nette\Database\Context $database) {
		$this->database = $database;
	}

	public function uctyGet($parTypek) {

presenter:

<?php

namespace App\Presenters;

use Nette;
use App\Model\Model;



class HomepagePresenter extends BasePresenter
{

	/** @var Nette\Database\Context */
	private $database;


	public function __construct(Nette\Database\Context $database)
	{
		$this->database = $database;

	}

	public function renderUcty($parTypek = 'P')	{

		$this->template->ucty = $this->context->getService('ModelX')->uctyGet($parTypek);

	}

but i get error Service ‘ModelX’ not found. search►

can you help me please

tahnk you Tomas Holy

CZechBoY
Member | 3598
+
0
-

You should not use $this->context in presenter.
Use dependency injection:

class HomepagePresenter extends BasePresenter
{
    private $database;
    private $uctyModel;

    public function __construct(Context $database, ModelX $uctyModel)
    {
        parent::__construct(); // always call parent constructor!

        $this->database = $database;
        $this->uctyModel = $uctyModel;
    }

    public function renderUcty($parTypek = 'P') {

        $this->template->ucty = $this->uctyModel->uctyGet($parTypek);

    }
}
thcom
Backer | 72
+
0
-

i try to fix it, but now i get error:

Service ‘application.4’: Class App\Presenters\Context needed by App\Presenters\HomepagePresenter::__construct() not found. Check type hint and ‘use’ statements. search►

CZechBoY
Member | 3598
+
0
-

Add

use Nette\Database\Context;

after namespace declaration.