Knihovna pro objektově relační mapování v PHP.
Doctrine home page: http://www.doctrine-project.org/
Demo examples: http://doctrine.isn.cz/
Protože mám v konfiguraci PHP (/etc/php.ini) volbu allow_url_fopen=Off, tak jsem do PHP CLI povolil hodnotu On pro stažení a instalaci.
Stažení
shell# mkdir -p /data/install/doctrine shell# cd /data/install/doctrine shell# curl -s http://getcomposer.org/installer | php -d allow_url_fopen=On
Vytvoření konfiguračního souboru pro Composer
Pro verzi Doctrine 2.3.4.
{ "require": { "doctrine/orm": "2.3.4" } }
Instalace přes Composer
shell# php -d allow_url_fopen=On composer.phar install
Namespace: Doctrine\Common
Doc: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/
Včetně Common.
Namespace: Doctrine\DBAL
Doc: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/
Včetně Common a DBAL.
Namespace: Doctrine\ORM
Doc: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/
Vytvoření MySQL databáze a nastavení oprávnění.
shell# mysql -p -u root mysql> CREATE DATABASE doctrinedemo DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; mysql> GRANT ALL PRIVILEGES ON doctrinedemo.* TO doctrinedemo@localhost IDENTIFIED BY 'secretPassword'; mysql> FLUSH PRIVILEGES; mysql> QUIT;
Přejít do adresáře projektu.
shell# mkdir -p lib/doctrine shell# cp -r /data/install/doctrine/vendor ./lib/doctrine
<?php // bootstrap.php require_once './lib/vendor/autoload.php'; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; $paths = array('./lib/entities'); $isDevMode = false; // the connection configuration $dbParams = array( 'driver' => 'pdo_mysql', 'user' => 'doctrinedemo', 'password' => 'secretPassword', 'dbname' => 'doctrinedemo', ); $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); $entityManager = EntityManager::create($dbParams, $config); ?>
<?php /** * Description of User * * @author blazek * @Entity @Table(name="user") */ class User { /** * @Id @Column(type="integer") * @GeneratedValue */ private $id; /** @Column(type="string", length=45, unique=false, nullable=true) */ private $firstname; /** @Column(type="string", length=45, unique=false, nullable=true) */ private $lastname; /** @Column(type="string", length=45, unique=true, nullable=false) */ private $login; /** @Column(type="string", length=64, unique=false, nullable=false) */ private $password; /** @Column(type="text", nullable=false) */ private $description; /** * @Column(type="datetime", nullable=false) * @GeneratedValue */ private $insertDate; function __construct() { } public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getFirstname() { return $this->firstname; } public function setFirstname($firstname) { $this->firstname = $firstname; } public function getLastname() { return $this->lastname; } public function setLastname($lastname) { $this->lastname = $lastname; } public function getLogin() { return $this->login; } public function setLogin($login) { $this->login = $login; } public function getPassword() { return $this->password; } public function setPassword($password) { $this->password = $password; } public function getDescription() { return $this->description; } public function setDescription($description) { $this->description = $description; } public function getInsertDate() { return $this->insertDate; } public function setInsertDate($insertDate) { $this->insertDate = $insertDate; } } ?>
-- ----------------------------------------------------- -- Table `doctrinedemo`.`user` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `doctrinedemo`.`user` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `firstname` VARCHAR(45) NULL, `lastname` VARCHAR(45) NULL, `login` VARCHAR(45) NOT NULL, `password` VARCHAR(64) NOT NULL, `description` TEXT NULL, PRIMARY KEY (`id`)) ENGINE = InnoDB;