Created
May 17, 2011 08:30
-
-
Save fprochazka/976155 to your computer and use it in GitHub Desktop.
Psáno na aktuální Nette (master - 50c3c40)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class BaseModel extends Nette\Object | |
{ | |
/** @var NotORM */ | |
protected $connection; | |
/** | |
* @param NotORM | |
*/ | |
public function __construct(NotORM $connection) | |
{ | |
$this->connection = $connection; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
common: | |
service: | |
connection: App\MyServiceFactories::createServiceConnection | |
models: App\MyServiceFactories::createServiceModels | |
development < common: | |
database: | |
engine: mysql | |
host: localhost | |
database: foo | |
username: foo | |
password: bar | |
charset: utf8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FooModel extends BaseModel | |
{ | |
public function find($id) | |
{ | |
// ... | |
return $result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FooPresenter extends BasePresenter | |
{ | |
protected $foo = NULL; | |
public function actionDetail($id) | |
{ | |
// tady přibyla jedna property v řetězci | |
// Models je Locator na všechny služby v MyModelsLocator | |
$this->foo = $this->context->models->foo->find($id); | |
if (!$this->foo) { | |
throw new \Nette\Application\BadRequestException("Foo with id $id not found"); | |
} | |
} | |
public function renderDetail() | |
{ | |
$this->template->foo = $this->foo; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @property-read FooModel $foo | |
*/ | |
class MyModelsLocator extends Nette\DI\Container | |
{ | |
protected function createServiceFoo() | |
{ | |
// všimni si že služba container se sem dostane v továrničce v MyServiceFactories | |
return new FooModel($this->container->connection); | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyServiceFactories | |
{ | |
public static function createServiceConnection(IContainer $container) | |
{ | |
$db = $container->config['database']; | |
$pdo = new PDO("{$db['engine']}:host={$db['host']};dbname={$db['database']}", $db['username'], $db['password']); | |
if (isset($db['charset'])) { | |
$pdo->exec("SET CHARACTER SET " . $db['charset']); | |
} | |
return new NotORM($pdo); | |
} | |
public static function createServiceModels(IContainer $container) | |
{ | |
$models = new MyModelsLocator(); | |
$models->addService('container', $container); // kvůli connection, globálním službám, ... | |
return $models; | |
} | |
} | |
// .., app run |
Máš spoustu možností
- Můžeš si udělat nějaký filter iterator nad třídami z robotLoaderu a udělat si seznam modelů a ty pak mít v cachi a vždycky je registrovat :)
- Nebo si uděláš magické udělátko https://github.com/nette/nette/blob/master/Nette/common/Configurator.php#L41 jako v nette
- nebo to uděláš podobně, jako je to v nejnovější revizi tohohle gistu :)
- ...
Díky, už tomu začínám přicházet na chuť : )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A když těch modelů bude víc? Dá se teď v Nette udělat ta registrace automaticky? Tzn. aby pro každý model nemusel být kód
createServiceFoo
a definice vconfig.neon
?