Created
July 27, 2011 15:22
-
-
Save Ginny/1109602 to your computer and use it in GitHub Desktop.
Model loader Nette
This file contains hidden or 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 BasePresenter extends Nette\Application\UI\Presenter { | |
| public function getModel($model) { | |
| return $this->context->modelLoader->loadModel($model); // umozni nam v presenterech nacitat model ve tvaru: $this->getModel('foo') | |
| } | |
| } | |
| ?> |
This file contains hidden or 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
| services: | |
| database: | |
| class: Nette\Database\Connection | |
| arguments: [sqlite2:%appDir%/models/demo.db] | |
| modelLoader: | |
| class: ModelLoader | |
| arguments: ["@database"] |
This file contains hidden or 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 | |
| use Nette\Database\Connection, | |
| Nette\DI\Container; | |
| class ModelLoader { | |
| private $connection; | |
| public function __construct(Nette\Database\Connection $connection) { | |
| $this->connection = $connection; | |
| } | |
| public function loadModel($modelName) { | |
| $class = "Model\\" . $modelName; | |
| return new $class($this->connection); | |
| } | |
| } | |
| ?> |
This file contains hidden or 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 | |
| namespace Model; | |
| use Nette\Object; | |
| /** | |
| * Model base class. | |
| */ | |
| class PagesModel extends Object { | |
| /** @var Nette\Database\Connection */ | |
| public $database; | |
| public function __construct($database) { | |
| $this->database = $database; | |
| } | |
| public function getPages() { | |
| return $this->database->table('pages'); | |
| } | |
| ?> |
This file contains hidden or 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 | |
| ... | |
| $this->pages = $this->getModel('PagesModel')->getPages(); | |
| ... | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment