Obsah
Doctrine 2
Knihovna pro objektově relační mapování v PHP.
Doctrine home page: http://www.doctrine-project.org/
Demo examples: http://doctrine.isn.cz/
Požadavky
- miminálně PHP 5.3.0
Instalace pomocí Composeru
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.
- composer.json
{ "require": { "doctrine/orm": "2.3.4" } }
Instalace přes Composer
shell# php -d allow_url_fopen=On composer.phar install
Vrstvy
Common
Namespace: Doctrine\Common
Doc: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/
- Class Loading
- Doctrine Annotations
- Doctrine Caching
DBAL (DataBase Abstraction Layer)
Včetně Common.
Namespace: Doctrine\DBAL
Doc: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/
- PDO
- DQL (Doctrine Query Language)
ORM (Object-Relational Mapping)
Včetně Common a DBAL.
Namespace: Doctrine\ORM
Doc: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/
MySQL DB
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říprava a konfigurace
Přejít do adresáře projektu.
shell# mkdir -p lib/doctrine shell# cp -r /data/install/doctrine/vendor ./lib/doctrine
- doctrine-load.php
<?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); ?>
Entity
- entities/User.php
<?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; } } ?>
- create.sql
-- ----------------------------------------------------- -- 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;