初探PHP ORM框架Doctrine

概述

环境:debian
Doctrine是PHP的一个ORM框架,symfony的默认用的ORM就是Doctrine。 

安装Composer

首先安装Composer,Composer是PHP依赖管理工具,类似Debian的apt,Java的Maven,安装说明可参考:http://docs.phpcomposer.com/00-intro.html

mkdir composer
curl -sS https://getcomposer.org/installer | php

这样就把composer.phar下载了到了composer文件夹了。

为了目录根清晰,我们新建一个和composer同级的目录doctrine.

cd .. && mkdir doctrine && cd doctrine

在doctrine文件夹里,编写依赖文件composer.json,内容如下:

{
"require": {
"doctrine/orm": "2.4.*",
"symfony/yaml": "2.*"
},
"autoload": {
"psr-0": {
"": "src/"
}
}
}

安装依赖

../composer/composer.phar install

有时会报异常:

[Composer\Downloader\TransportException]
Your configuration does not allow connection to
http://packagist.org. See https://getcomposer.org/doc/06-config.md#secure-http
for details.

可以多试几次,如果还不行可以尝试添加一个配置项:

{
"require": {
"doctrine/orm": "2.4.*",
"symfony/yaml": "2.*"
},
"autoload": {
"psr-0": {
"": "src/"
}
},
"config": {
"secure-http": false
}
}

安装完依赖后,如果你尝试运行 vendor/bin/doctrine,会得到类似这样的提示:

You are missing a “cli-config.php” or “config/cli-config.php” file in your
project, which is required to get the Doctrine Console working. You can use the
following sample as a template:

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;

// replace with file to your own project bootstrap
require_once ‘bootstrap.php’;

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

return ConsoleRunner::createHelperSet($entityManager);

告诉缺少cli-config.php文件,于是添加如下两个文件bootstrap.php和cli-config.php:

<?php
use Doctrine\ORM\Tools\Setup;

require_once "vendor/autoload.php";

// Create a simple "default" Doctrine ORM configuration for XML Mapping
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
// or if you prefer yaml or annotations
//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);

// database configuration parameters
/*$conn = array(
'driver' =&gt; 'pdo_sqlite',
'path' =&gt; __DIR__ . '/db.sqlite',
);*/
$conn = array(
'driver' =&gt; 'pdo_mysql',
'user' =&gt; 'root',
'password' =&gt; '',
'dbname' =&gt; 'foo',
);

// obtaining the entity manager
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);</pre>
<pre class="prettyprint lang-php">&lt;?php
// cli-config.php
require_once "bootstrap.php";

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'em' =&gt; new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager)
));

return $helperSet;

创建数据库和表

配置好bootstrap.php中的数据库以及目录(src),编写entity,如Product.php

<?php
/**
* @Entity @Table(name="products")
*/
class Product
{
/** @Id @Column(type="integer") @GeneratedValue */
protected $id;
/** @Column(type="string") */
protected $name;

public function getId()
{
return $this-&gt;id;
}

public function getName()
{
return $this-&gt;name;
}

public function setName($name)
{
$this-&gt;name = $name;
}
}

先登录Mysql创建数据库foo

create database foo;

然后可以执行

vendor/bin/doctrine orm:schema-tool:create

即可创建数据库表Products.

翟前锋 wechat
欢迎订阅我的微信公众号:zhaiqianfeng!