fabik/database

This package is abandoned and no longer maintained. The author suggests using the kdyby/doctrine package instead.

Database layer for Nette Framework.

1.1.0 2012-12-02 10:46 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:20:41 UTC


README

This is a database layer for Nette Framework based on Nette\Database.

Installation

Get the source code using Composer (add "fabik/database": "1.1.*" to your composer.json) or directly download Database to your libs directory.

Example of use

  1. Create the database:

    CREATE TABLE `users` (
    	`id` int unsigned NOT NULL AUTO_INCREMENT,
    	`username` varchar(255) NOT NULL,
    	`password` char(40) NOT NULL,
    	`email` varchar(255) NOT NULL,
    	`firstname` varchar(255) NOT NULL,
    	`surname` varchar(255) NOT NULL,
    	PRIMARY KEY (`id`),
    	UNIQUE KEY (`username`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE `articles` (
    	`id` int unsigned NOT NULL AUTO_INCREMENT,
    	`title` varchar(255) NOT NULL,
    	`content` longtext NOT NULL,
    	`author_id` int unsigned NOT NULL,
    	PRIMARY KEY (`id`),
    	KEY (`author_id`),
    	FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
    ) ENGINE=InnoDB;
  2. Register the compiler extension in the bootstrapper.

    use Fabik\Database\DatabaseExtension;
    
    $configurator->onCompile[] = function($configurator, $compiler) {
    	$compiler->addExtension('database', new DatabaseExtension);
    };
  3. Add the following sections to your config.neon file:

    nette:
    	database:
    		default:
    			dsn: '%database.driver%:host=%database.host%;dbname=%database.dbname%'
    			user: %database.user%
    			password: %database.password%
    
    database:
    	rowFactory:
    		classes:
    			articles: Blog\Article
    			users: Blog\User
    
    services:
    	articles: Blog\Articles
    	users: Blog\Users
  4. Create classes for rows (e.g. Article, User) and tables (e.g. Articles, Users):

    <?php
    
    namespace Blog;
    
    use Fabik\Database\ActiveRow,
    	Fabik\Database\Table;
    
    
    
    class Article extends ActiveRow
    {
    }
    
    
    
    class User extends ActiveRow
    {
    	/** @return string */
    	public function getRealname()
    	{
    		return "$this->firstname $this->surname";
    	}
    
    
    
    	/** @param string */
    	public function setRealname($realname)
    	{
    		list($this->firstname, $this->surname) = explode(' ', $realname);
    	}
    }
    
    
    
    class Articles extends Table
    {
    	protected $name = 'articles';
    }
    
    
    
    class Users extends Table
    {
    	protected $name = 'users';
    }
  5. Now you can use it as follows:

    $articles = $container->articles;
    
    foreach ($articles->findAll() as $article) {
    	echo "$article->title was written by $article->author->realname\n";
    }