您的位置:首页 > 其它

doctrine安装和配置

2020-01-15 08:57 80 查看

1.doctrine2现在使用composer进行快捷的安装。在项目目录中增加一个composer.json文件,内容如下:

{
"require" : {
"doctrine/orm" : "*" // 或者"v2.5.*"
}
}

然后在命令行中运行 composer install 进行安装。composer安装请参考 composer 安装
2.安装完成后,可以在项目的入口文件中直接使用composer提供的自动加载类进行扩展包中类的自动加载。使用require_once “paht\vendor\autoload.php”。
3.要使用doctrine提供的ORM功能,首先必须先实例化一个实体管理器EntityManager。实例化代码如:

require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
// 指定entity文件存放位置
$paths = array("/path/to/entity-files");
// 指定开发模式
$isDevMode = false;
// 设置数据库连接参数
$dbParams = array(
'driver'   => 'pdo_mysql',
'user'     => 'root',
'password' => 'root',
'dbname'   => 'doctrine',
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

如果是开发模式为true时,缓存将使用文件数组进行缓存,关联对象将在每次的请求重新进行创建;如果开发模式是false时,将会自动按照APC,Xcache,Memcache,redis的顺序查找缓存数据,直到无法找到时,从数据库获取;代理类必须通过命令行进行显示的创建,如果代理类存放文件夹不存在时使用系统的临时目录。

4.doctrine 提供了很多有用的命令行工具,可以使用此命令进行调用 php vendor/bin/doctrine。要使用此这些命令,你可以在项目目录中创建一个cli-config.php文件,来让实例化好的实体管理器注册到命令工具中,文件内容如下:

use Doctrine\ORM\Tools\Console\ConsoleRunner;
// 项目入口文件,里面包含一个创建好的实体管理器
require_once 'index.php';
return ConsoleRunner::createHelperSet($entityManager);

映射

在doctrine中通过实体对象Entity 来进行数据的存取,文件中通过文档块注释来定义映射的元数据结构来指定数据如何进行存取。如以下的用户表例子:

/**
* @Entity
* @Table(name="users")
*/
class User
{
/**
* 声明此字段为主键字段,自增及使用int类型
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
private $id;
/**
* 用户名
* @Column(type="string", length=36)
*/
private $username;
/**
* 密码(32位char类型)
* @Column(type="string", columnDefinition="char(32) not null default '' ")
*/
private $password;
/**
* 手机号码(11位char类型,唯一索引)
* @Column(type="string",  unique=true, columnDefinition="char(11) not null default '' ")
*/
private $mobile;
/**
* 性别
* @Column(type="boolean")
/*
private $gender;
/**
* 出生日期
* @Column(type="date")
/*
private $birthday;
/**
* 积分
* @Column(type="integer")
*/
private $integral;
/**
* 最后登录时间(可用integer)
* @Column(type="datetime")
*/
private $last_login;
/**
* 余额
* @Column(type="decimal", precision=10, scale=2)
*/
private $balance;
}

name:指定映射的数据库列名称,不指定时默认使用属性名。
type:指定映射的数据库中列的类型。不指定时默认为string类型即对应mysql的varchar(255)。
length:指定列类型是string时的长度,如:length=32对应mysql的varchar(32),不指定时默认为255。
unique: 指定列添加唯一索引,默认为false。
nullable: 指定列是否可空,默认为false。
precision:指定列为decimal类型时,可存储的最大值。
scale: 指定列位decimal类型时,小数点后的精度位数。
columnDefinition:直接使用数据库定义语句DDL片段进行指定。如:columnDefinition="char(32) not null "。
options: 通过键值对参数设置数据库定义语言参数。如:options=[ “nullable” => false, “unsigned” => true, “default” => ‘’];

基本映射类型(Mysql):
string => varchar; integer => int; smallint => smallint; bigint => bigint;
boolean => tinyint; decimal => decimal; date => datetime; time => time;
datetime => timestamp;
text => text;
object => text(serialize()/unserialize());
array => text(serialize()/unserialize());
simple_array => text(implode()/explode());
json_array => text(json_encode()/json_decode())

  • 点赞
  • 收藏
  • 分享
  • 文章举报
邓首长在春天的故事 发布了0 篇原创文章 · 获赞 1 · 访问量 249 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: