Here you have a easy way to connect to database .. Thanks to Oli help

Notice: This thread is very old.
alnux
Member | 106
+
0
-

Here you have a way to connect to database

setTables.php

/**
 * Set tables in just one place
 *
 * @author  Copyright (c) 2014 Leonardo D. A. Allende Pastén (alnux)
 */
namespace App\Model;

use \Nette\Object;
/**
 * Set tables
 */
class SetTables extends Object
{
	// Set a const table in this case paises
	const T_PAISES = 'paises';
}

BaseModel.php Model class

/**
 * @author  Copyright (c) 2014 Leonardo D. A. Allende Pastén (alnux)
 */
namespace App\Model;

use \Nette\Database\Context;
use App\Model\AjustarTablas;
/**
 * Base Model.
 */
class BaseModel extends SetTables {

	private $context;

	protected $table;

	/**
	 * Get dataBase Connection
     *
	 * @param Nette\Database\Context $database connection object var to database
	 */
    public function __construct(Context $context)
    {
        $this->context = $context;
    }


    /**
     * Return all items
     *
     * @return Nette\Database\Table\Selection
     */
    public function all()
    {
       return $this->context->table($this->table);
    }

Paises.php Model class

/**
 * @author  Copyright (c) 2014 Leonardo D. A. Allende Pastén (alnux)
 */
namespace App\Model;

use App\Model\BaseModel;
/**
 * Clase Modelo Paises.
 */
class Paises extends BaseModel {

	protected $table = AjustarTablas::T_PAISES;

}

The next class connect database to presenters

/**
 * @author  Copyright (c) 2014 Leonardo D. A. Allende Pastén (alnux)
 */
namespace App\Model;

use \Nette\DI\Container,\Nette\Database\Context;
use App\Model\Paises;
/**
 * Models container
 */
class ModeloContenedor
{

    private $context;

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

    public function getPaises()
    {
        return new Paises($this->context);

    }

}

and finally config.neon configuration

.
.
.
.
services:
    modeloContenedor: App\Model\ModeloContenedor

From presnter you get it using @inject annotation on a public member variable more info here , please put attention on
/** @inject @var \App\Model\ModeloContenedor */ before var with out this does not will work

namespace App\Presenters;

use Nette,
	App\Model\ModeloContenedor;


/**
 * Homepage presenter.
 */
class HomepagePresenter extends BasePresenter
{

   /** @inject @var \App\Model\ModeloContenedor */
    public $modeloContenedor;

	public function renderDefault()
	{
		$paises = $this->modeloContenedor->getPaises();
		$this->template->paises = $paises->all();
	}

}

Last edited by alnux (2014-06-13 17:11)