Rewriting Quickstart to use Model

Notice: This thread is very old.
McLel
Member | 12
+
0
-

Hello again. I've finished quickstart and I'm trying to rewrite it so it uses Model. But I can't get it to work. For starters, I wanted to rewrite the homepage:

presenters/HomepagePresenter.php:

namespace App\Presenters;

use Nette;
use App\Model\HomepageModel;

class HomepagePresenter extends BasePresenter
{
    public $homepagemodel;

    public function renderDefault() {
        $this->template->posts = $this->homepagemodel->getArticles();
    }
}

model/HomepageModel.php:

namespace App\Model;

use Nette;

class HomepageModel extends Nette\Object
{
	/**
	 * @var Nette\Database\Context
	 */
	private $database;

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

	public function getArticles()
	{
		return $this->database->table('news')->order('id DESC')->limit(5);
		//I renamed the table to 'news'
	}
}

Config.neon:

services:
	- App\Model\UserManager
	- App\Model\HomepageModel
	- App\Forms\SignFormFactory
	router: App\RouterFactory::createRouter

I am getting this error:

Nette\DI\ServiceCreationException

Class App\Model\HomepageModel used in service ‘26_App_Model_HomepageModel’ not found or is not instantiable.

I haven't changed anything in Latte yet. What am I missing?

Last edited by McLel (2015-11-17 15:32)

Šaman
Member | 2525
+
0
-

Is your /model in /app? It looks like RobotLoader cant find class App\Model\HomepageModel. Check all names and namespaces. I cant see error in this in your posted code.

But you have another error – you dont inject HomepageModel to presenter correctly. Change to:

<?php
/**
	@var HomepageModel
	@inject
*/
public $homepagemodel;
?>

And name HomepageModel is VERY bad name for one class of model. NewsModel, or better NewsRepository or NewsService will be better.


edit:
You use use section (use App\Model\HomepageModel), so only HomepageModel is need in @var annotation.
I change it.

Last edited by Šaman (2015-11-17 15:13)

McLel
Member | 12
+
0
-

Thanks for lightning fast response, I will change the name later. I added the injection.

However, I doublechecked and I do have HomepageModel.php in sandbox/app/model

McLel
Member | 12
+
+2
-

Ok, problem solved, I deleted Cache and it works now :-)