Nette Models not mentioned in the documentation index

Notice: This thread is very old.
simonjcarr
Member | 28
+
0
-

The Nette documentation does not mention the use of models. Does this mean that it is recommended to place ORM code within the presenter? If not, can anyone point me to the recommended method of implementing models in my nette application.

Thanks

Spectator
Member | 48
+
0
-

ORM is usually added as extension. You can use existing orm and extensions for nette.

Doctrine2:
https://github.com/Kdyby/Doctrine

Lean Mapper:
http://www.leanmapper.com/
https://github.com/…er-extension

Nextras orm:
https://nextras.org
https://github.com/…xtras-ormext

Propel, …

simonjcarr
Member | 28
+
0
-

Sorry spectator, there might be some confusion. My question is about model files. Are they recommended by Nette or not? There is no mention of them in the documentation. All the database access in the documentation is done via the Presenter.

Jan Tvrdík
Nette guru | 2595
+
+1
-

Are they recommended by Nette or not?

Yes, they are. Just as for any other framework.

thundervoice
Member | 10
+
0
-

Yes i can, because i had the same Situation.

I've done this for a JSON Response like this:

A Model Class File in the Folder model named MandatorModel.php

<?php

namespace App\Model;

use Nette;
use App\Model;

class MandatorModel extends Nette\Object
{
	private $database;
	private $session;

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

	/**
	* You should use try and catch
	* @param int $id the ID will search the right Record
	* return obj the dataset for Mandator
	*/
	public function getMandator($id){
		$data = new \stdClass(); // This Class is the Return Object

		try{
			// If database is connected and no Failures throwing
			// Overwrite $data
			$data = $this->database->query("SELECT * FROM mandators WHERE mnd = $id")->fetch();

			if(!$data){ // $data = false because it's empty
				// This will throw a self named Exception (show down the Class MandatorEmptyException)
				// You can add a Number in this Exception for to Proceed further
				throw new MandatorEmptyException("Message",1);
			} else {
				return $data;
			}
		} catch (\PDOException $e) {
			// This Catch Exception will get when PDO-Driver fails
			$data = new \stdClass();
			$data->code = $e->getCode();
			$data->message = $e->getMessage();
			$data->error = true;
			return $data;
		}
	}
}

Class MandatorEmptyException extends \Exception
{}

The Presenter is Important for the Settings in Head-Content

I've used the BasePresenter and extend them. Like this in MandatorPresenter.php

<?php

namespace App\Presenters;

use Nette;
use App\Model;
use Nette\Security\User;


class MandatorPresenter extends BasePresenter
{

	public $httpRequest;
	public $post;
	private $mnd;

	public function __construct(Model\MandatorManager $mnd, Nette\Http\IRequest $httpRequest) {
		$this->httpRequest = $httpRequest;
		$this->post = $this->httpRequest->getPost();
		$this->mnd = $mnd; // MandatorModel
	}

	/* Set the Actions
	 * Important is Prefix action
	 * and the Parameter must named $id
	 * Actions mustn't have a latte File
	 * @param int $id
	*/
	public function actionGet($id){
		$data = new \stdClass(); // send Class $data as JSON Response

		// Call the MandatorModel Function
		$data->mandator = $this->mandator->getMandator($id);

		$this->sendJson($data);
	}
}

The URI will be like this:

yourhost.com/mandator/123456
This get the Mandator as JSON Response

Have a lot of Fun!

Thanks the Guys they teached me here!

Last edited by thundervoice (2016-10-22 10:03)

chemix
Nette Core | 1304
+
0
-

There is a part in documentation, where the model is mentioned https://doc.nette.org/…kstart/model

CZechBoY
Member | 3608
+
0
-

@thundervoice btw HttpRequest is already in presenter, you can access it first in startup method.

private $post = [];

public function startup()
{
	$this->post = $this->getHttpRequest()->getPost();
}