local.neon
database:
clanky:
dsn: 'mysql:host=localhost;dbname=clanky'
user: root
password:
options:
lazy: yes
komentare:
dsn: 'mysql:host=localhost;dbname=komentare'
user: root
password:
options:
lazy: yes
services.neon
services:
# údaj v závorkách určuje, které připojení definované výš třída dostane v konstruktoru
- App\Model\Clanky(@database.clanky.context)
- App\Model\Komentare(@database.komentare.context)
# Tento zápis je také funkční
# Pokud totiž není určeno, které připojení se má použít, použije se první definované připojení
# - App\Model\Clanky
# - App\Model\Komentare(@database.komentare.context)
# Také funkční
# - App\Model\Komentare(@database.komentare.context)
# - App\Model\Clanky
Clanky.php
<?php
namespace App\Model;
use Nette;
class Clanky{
/** @var Nette\Database\Context */
private $db;
public function __construct(Nette\Database\Context $db){
$this->db = $db;
}
public function get(int $id){
return $this->db->table('clanky')->get($id);
}
}
Komentare.php
<?php
namespace App\Model;
use Nette;
class Komentare{
/** @var Nette\Database\Context */
private $db;
public function __construct(Nette\Database\Context $db){
$this->db = $db;
}
public function getKomentare(int $clanek_id){
return $this->db->table('komentare')->where('clanek_id', $clanek_id);
}
}
ClanekPresenter.php
<?php
namespace App\Presenters;
use Nette;
use App;
class ClanekPresenter extends Nette\Application\UI\Presenter{
/** @var App\Model\Clanky @inject */
public $clanky;
/** @var App\Model\Komentare @inject */
public $komentare;
public function renderDefault(int $id){
//načte článek z databáze 'clanky'
$this->template->clanek = $this->clanky->get($id);
//načte komentáře z databáze 'komentare'
$this->template->komentare = $this->komentare->getKomentare($id);
}
}