aspendigital/fuel-doctrine2

FuelPHP 框架下对 Doctrine2 的集成

1.0.2 2013-07-02 21:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 11:53:38 UTC


README

关于

此包包含了一个基本的包装器,用于通过 FuelPHP 框架访问 Doctrine 2 ORM 功能。它遵循与 Doctrine 本身相同的 LGPL 许可。

如何安装

您可以使用 Composer 安装此包。因此,首先您需要 Composer,可以在 https://getcomposer.org.cn/ 找到它

  1. 如果您的应用程序没有 composer.json 文件,请创建它。
  2. 添加额外的 require
"aspendigital/fuel-doctrine2": "dev-master"
  1. 使用 composer install 安装

快速入门

配置简单,只需将额外的 Doctrine 配置选项添加到您的 FuelPHP db.php 配置文件中。

为了快速运行,您只需要在 app/config/db.php 中添加以下三个必需设置

添加以下内容

'doctrine2'=>array(
	'proxy_dir' => APPPATH . 'classes' . DS . 'proxy',
	'proxy_namespace' => 'Proxy',
	'metadata_path' => APPPATH . 'classes' . DS . 'entity'
)

当然,根据您通常为 Fuel 配置的方式,配置数据库设置(用户、密码等)。

如何使用

在配置您的应用程序后,要获取 EntityManager,请使用以下代码

$em = \Fuel\Doctrine::manager(); // Uses the connection referred to by the 'active' index in your configuration
$em = \Fuel\Doctrine::manager('connection_2'); // Specify connection explicitly

或者,您也可以检查 Doctrine 组件的版本

print_r(\Fuel\Doctrine::version_check());

典型配置示例

使用 FuelPHP 提供的级联配置文件,典型的配置看起来如下

app/config/db.php:

return array(
	'active'=>'default',

	'doctrine2'=>array(
		'proxy_dir'       => APPPATH . 'classes' . DS . 'proxy',
		'proxy_namespace' => 'Proxy',
		'metadata_path'   => APPPATH . 'classes' . DS . 'entity',
		'metadata_driver' => 'annotation',
		'init_callback'   => array('MyClass', 'init')
	)

	/**
	 * Base config, just need to set the DSN, username and password in env. config.
	 */
	'default' => array(
		'type'        => 'pdo',
		'connection'  => array(
			'persistent' => false,
			'compress'   => false
		),
		'charset'      => 'utf8',
		'profiling'    => false
	)
);

app/config/development/db.php:

return array(
	'doctrine2'=>array(
		'auto_generate_proxy_classes' => true
	),

	'default'=>array(
		'connection'  => array(
			'dsn'            => 'pgsql:host=localhost;dbname=fuel_db',
            'username'       => 'your_username',
            'password'       => 'y0uR_p@ssW0rd'
		)
		'profiling'   => true
	)
);

app/config/production/db.php:

return array(
	'doctrine2'=>array(
		'auto_generate_proxy_classes'   => false,
		'cache_driver'                  => 'apc'
	),

	'default'=>array(
		'connection'  => array(
			'dsn'            => 'pgsql:host=production_server;dbname=fuel_db',
            'username'       => 'your_username',
            'password'       => 'y0uR_p@ssW0rd'
		)
		'profiling'    => false
	)
);

在开发环境中,我们使用默认的数组缓存(没有数据永久保存)并启用分析。在生产环境中,我们关闭分析并使用 APC(或某些其他缓存解决方案)。

连接设置覆盖

如果由于某些原因您需要根据连接逐个覆盖 Doctrine2 设置,请在连接设置中包含一个 doctrine2

return array(

	'default'=>array(
		'connection'  => array(
			'dsn'            => 'pgsql:host=production_server;dbname=fuel_db',
            'username'       => 'your_username',
            'password'       => 'y0uR_p@ssW0rd'
		)
		'profiling'    => false,
		'doctrine2'    => array(
			'cache_driver'   => 'zend' // Override the cache driver only for the 'default' connection
		)
	)
);

配置选项

请参阅 Doctrine 2 文档

  • proxy_dir:包含您的代理类的目录
  • proxy_namespace:代理类所在的命名空间
  • metadata_path:包含您的元数据的目录
  • metadata_driver:选项为 'annotation'(默认)、'php'、'simplified_xml'、'simplified_yaml'、'xml'、'yaml' 等
  • auto_generate_proxy_classes:true/false,用于指定 Doctrine 是否为加载的实体生成代理类
  • cache_driver:选项为 'array'(默认)、'apc'、'xcache'、'wincache'、'zend' 等
  • init_callback:一个 'callable' 值,当 EntityManager 被实例化时会运行。这可以用来设置自定义 DBAL 类型或以其他方式自定义环境。

app/classes/myclass.php:

class MyClass
{
	/**
	 * @param \Doctrine\ORM\EntityManager $manager
	 * @param string $connection
	 */
	public static function init($manager, $connection)
	{
		...
	}
}

在连接时

  • driver:我们尝试猜测要加载以连接到您的数据库的 DBAL 驱动,但如果猜测不适用于您,则可能需要设置此值
  • 有关其他 DBAL 特定选项,请参阅 Doctrine DBAL 文档

有关 FuelPHP 选项,请参阅 Fuel 数据库配置

  • 类型
  • 字符集
  • 分析
  • enable_cache:在我们的例子中,始终会进行一些缓存,但如果您已更改 cache_driver 设置,则它只是临时的
  • connection.persistent
  • connection.compress

分析

除了为您的连接启用分析之外,无需其他配置。通过 Doctrine ORM 和直接通过 DBAL 发送的查询将自动出现在 Fuel 分析器中。

Doctrine 2 ORM

Doctrine 2 是一个针对 PHP 5.3.2+ 的对象关系映射(ORM)工具,为 PHP 对象提供透明的持久化功能。它建立在强大的数据库抽象层(DBAL)之上。其关键特性之一是可选地使用一种名为 Doctrine 查询语言(DQL)的专有面向对象 SQL 语法来编写数据库查询,这种语法受到了 Hibernate HQL 的启发。这为开发者提供了一种功能强大的 SQL 替代方案,同时保持了灵活性,无需进行不必要的代码重复。

更多资源