Nette\Database 2.2.2 problem s pripojenim do dvou DB
- saimons
- Člen | 293
V prubehu behu jednoho reuestu se potrebuju pripojit do vice DB, viz priklad. Problem je v tom, ze vsechno vypada v poradku, Context obsahuje spravna pripojovaci data, ale na konci v Connection se najednou objevi jina. Nedava mi to smysl. Tady je moje konstrukce pripojeni.
config.neon
nette:
database:
smcore:
dsn: '%database.smcore.driver%:host=%database.smcore.host%;dbname=%database.smcore.dbname%'
user: %database.smcore.user%
password: %database.smcore.password%
smkredit:
dsn: '%database.smkredit.driver%:host=%database.smkredit.host%;dbname=%database.smkredit.dbname%'
user: %database.smkredit.user%
password: %database.smkredit.password%
services:
#Database
- Model\Database\smcore( @nette.database.smcore.context )
- Model\Database\smkredit( @nette.database.smkredit.context )
tridy pro vytvoreni pripojeni, obe dve jsou stejne (smcore, smcredit):
<?php
namespace Model\Database;
use Nette\Database\Context;
class smkredit /*smcore*/ {
/**
* @var Nette\Database\Context
*/
private $database;
/**
*
* @param \Nette\Database\Context $context
*/
public function __construct(Context $context) {
$this->database = $context;
}
/**
* @return Nette\Database\Context
*/
public function getContext() {
return $this->database;
}
}
Pote mam dve tridy ktere jsou volane po sobe z presenteru:
class Database extends Object {
/**
* @var \Nette\Database\Context
*/
private $database;
/**
* @param \Model\Database\smkredit $database
*/
public function __construct(\Model\Database\smkredit $database) {
$this->database = $database->getContext();
}
/**
* @return Nette\Database\Table\Selection
*/
private function findSluzba() {
return $this->database->table('sluzba');
}
}
class Database2 extends Object {
/**
* @var \Nette\Database\Context
*/
private $database;
/**
* @param \Model\Database\smcore $database
*/
public function __construct(\Model\Database\smcore $database) {
$this->database = $database->getContext();
}
/**
* @return Nette\Database\Table\Selection
*/
private function findSluzba2() {
return $this->database->table('sluzba2');
}
}
Pokud dam dump pred $this->database->table() na $this->database tak jsou tam spravna pripojovaci data, ale pote kdyz se vola Nette\Database\Connection tak jsou tam vzdy jen ta prvni. Mam nekde chybu, nebo kde by mohl byt problem? Predem dekuji za pomoc.
EDIT:
Po podrobnejsim prozkoumani jsem zjistil ze az do Database\Table\Selection a
jeho construkturu je vse v poradku, kde se vola $this->primary =
$reflection->getPrimary($table); a tam neni nikde predanej connection a trida
DiscoveredReflection si to v construkturu nekdud nacte, ale uz nezohledni,
ktera byla predtim v Selection. A je nactana vzdy ta prvni. Tak uplne nevim
kde by mohla byt chyba? V tom Nette\Database\Reflection\DiscoveredReflection
mam vzdy v connection to prvni spojeni.
Editoval saimons (4. 7. 2014 15:17)
- saimons
- Člen | 293
Tak uz jsem na to prisel, trida pro vytvoreni DB spojeni musi vypadat takto:
namespace Model\Database;
use Nette\Database\Context,
Nette\Database\Connection,
Nette\Database\Reflection\DiscoveredReflection,
Nette\Caching\IStorage;
/**
* Description of StadionmanagerDatabase
*
* @author stepan
*/
class smkredit {
/**
* @var Nette\Database\Connection
*/
private $connection;
/**
* @var Nette\Caching\IStorage
*/
private $cache;
/**
*
* @param \Nette\Database\Connection $connection
*/
public function __construct(Connection $connection, IStorage $cache) {
$this->connection = $connection;
$this->cache = $cache;
}
/**
* @return Nette\Database\Context
*/
public function getContext() {
$reflection = new DiscoveredReflection($this->connection, $this->cache);
return new Context($this->connection, $reflection, $this->cache);
}
}
a v configu volat jen – Model\Database\smkredit( @nette.database.smkredit ) bez Contextu