作者寄语:
一直在用Symfony框架,近期转到tp5,Model用着实在不习惯,自己简单整了个Doctrine玩玩
composer require doctrine/orm
2, 初始化配置Doctrine(1) cli-config.php
<?php
// E:/wamp/www/tp5/cli-config.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once 'vendor/autoload.php';
$config = require_once 'application/database.php';
$isDevMoe = true;
$configuration = Setup::createAnnotationMetadataConfiguration(array(__DIR__. '/application/entity'), $isDevMoe);
$conn = array(
'driver' => 'pdo_mysql',
'user' => $config['username'] ? $config['username'] : 'root',
'password' => $config['password'] ? $config['password'] : '',
'dbname' => $config['database'] ? $config['database'] : 'symfony',
'port' => $config['hostport'] ? $config['hostport'] : 3306,
'charset' => 'utf8'
);
$entityManager = EntityManager::create($conn, $configuration);
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
(2)Entity.php<?php
// E:/wamp/www/tp5/application/base/controller
namespace app\base\controller;
use think\Config;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
class Entity
{
public static function getEntityManager()
{
$isDevMoe = true;
$config = Setup::createAnnotationMetadataConfiguration(array(APP_PATH . '/entity'), $isDevMoe);
$conn = array(
'driver' => 'pdo_mysql',
'user' => Config::get('database.username') ? Config::get('database.username') : 'root',
'password' => Config::get('database.password') ? Config::get('database.password') : '',
'dbname' => Config::get('database.database') ? Config::get('database.database') : 'symfony',
'port' => Config::get('database.hostport') ? Config::get('database.hostport') : 3306,
'charset' => 'utf8'
);
return EntityManager::create($conn, $config);
}
}
定义Entity信息(1)Product.php
<?php
// .../application/entity/Product.php
namespace app\entity;
/**
* Class Product
* @package app\entity
*
* @Entity
* @Table(name="products")
*/
class Product
{
/**
* @var int
*
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
protected $id;
/**
* @var string
*
* @Column(type="string")
*/
protected $name;
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
}
(2)User.php<?php
namespace app\entity;
/**
* Class User
* @package app\entity
*
* @Entity
* @Table(name="users")
*/
class User
{
/**
* @var int
*
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
protected $id;
/**
* @var string
*
* @Column(type="string")
*/
protected $username;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
}
4,生成数据表,不用直接操作数据库,执行该命令后,会自动在数据库生成 products表和相关定义的字段id, name及字段属性vendor/bin/doctrine orm:schema-tool:update --force --dump-sql


5,插入数据
<?php
namespace app\index\controller;
use app\entity\Product;
use app\base\controller\Entity;
class Index
{
public function index()
{
return 'Hello world';
}
public function create(Entity $entity)
{
$product = new Product();
$product->setName('马宝2系');
$entityManager = $entity::getEntityManager();
$entityManager->persist($product);
$entityManager->flush();
return "Created Product with ID " . $product->getId();
}
}


可以看到,数据插入成功....................更多操作请自行观看Doctrine官方文档