pomm / pomm-bundle
用于Postgresql对象模型管理器的Symfony2包
Requires
- php: >=5.3.0
- pomm/pomm: ~1.2.0
- symfony/framework-bundle: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-07 18:27:54 UTC
README
什么是PommBundle(仅适用于Pomm 1.x)?
PommBundle使您能够从Pomm <http://pomm.coolkeums.org> 和 Postgres <https://postgresql.ac.cn> 功能中受益,这些功能从您的Symfony2 <http://www.symfony.com> 开发中受益。
注意
This bundle is only useful if you are using old version of Pomm 1.x. For other later versions please use the other bundle `pomm-project/pomm-bundle`.
安装
安装PommBundle有几种方法
- 使用Composer <http://www.packagist.org>。
- 克隆github仓库或在一个目录中下载PommBundle的文件。
Composer方法
只需将以下行添加到您的composer.json文件中
{ "minimum-stability": "dev", "require": { "pomm/pomm-bundle": "dev-master" } }
然后运行composer.phar install以在供应商目录中获取包,并设置自动加载器。如果您正在使用Symfony 2.0.x,您可能仍在使用sf2自动加载器。更新您的app/autoload.php文件
$loader->registerNamespaces(array( 'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'), ... 'Pomm' => __DIR__.'/../vendor/pomm/pomm', 'Pomm\\PommBundle' => __DIR__.'/../vendor/pomm/pomm-bundle',
下载文件
要使用PommBundle,您可以克隆或下载bundle 和 Pomm API,并将其放置在 vendor 目录中。
$ mkdir -p vendor/pomm/{pomm,pomm-bundle/Pomm/PommBundle} $ git clone https://github.com/chanmix51/Pomm vendor/pomm/pomm ... $ git clone https://github.com/chanmix51/PommBundle vendor/pomm/pomm-bundle/Pomm/PommBundle
现在,您必须告诉Symfony2自动加载器在哪里可以找到API和将要生成的文件。启动您的文本编辑器,并将以下行添加到 app/autoload.php 文件中
#app/autoload.php 'Pomm/PommBundle' => __DIR__.'/../vendor/bundles/Pomm', 'Pomm' => __DIR__.'/../vendor/pomm', # This is the default namespace for the model # But it can be changed see the command line tools 'Model' => __DIR__.'/..',
设置
让我们在应用程序内核中注册PommBundle
#app/AppKernel.php // register your bundles new Pomm\PommBundle\PommBundle(),
您现在可以在主配置文件中定义数据库设置。下面的例子使用了yaml格式
# app/config/config.yml pomm: databases: cnct_name: dsn: pgsql://user:password@host:port/dbname
这里的cnct_name是您数据库的名称。您可以使用不同的dsn或选项定义多个数据库。
#app/config/config.yml pomm: databases: con1: dsn: pgsql://user:password@host:port/dbname con2: dsn: pgsql://user:password@host:port/dbname class: My/Database # default: Pomm\Connection\Database isolation: SERIALIZABLE
现在,您可以配置安全层以进行用户认证
#app/config/security.yml security: providers: main: pomm: class: Acme\DemoBundle\Model\User property: email database: con1
您的用户模型必须实现UserInterface
#src/Acme/DemoBundle/Model/User.php namespace Acme\DemoBundle\Model; use Pomm\Object\BaseObject; use Symfony\Component\Security\Core\User\UserInterface; class User extends BaseObject implements UserInterface { public function getRoles() { return $this->get('roles'); } public function getPassword() { return $this->get('password'); } public function getSalt() { return $this->get('salt'); } public function getUsername() { return $this->get('email'); } public function eraseCredentials() { } }
如何注册转换器
您可以为所有数据库定义全局转换器定义,也可以为每个数据库定义
#app/config/config.yml pomm: converters: year: class: My\Pomm\Converter\Year types: [year] month: class: My\Pomm\Converter\Month types: [month] databases: con1: dsn: pgsql://user:password@host:port/dbname converters: day: class: My\Pomm\Converter\Day types: [day] con2: dsn: pgsql://user:password@host:port/dbname class: My/Database # default: Pomm\Connection\Database isolation: SERIALIZABLE
con1数据库将具有年、月和日转换器。con2数据库将具有年和月转换器。
如何生成Map文件
Map文件是Pomm了解您的表结构的方式。Pomm可以扫描数据库为您生成这些文件。
$ app/console pomm:mapfile:create my_table
这将创建一个包含MyTableMap类的文件Model/Pomm/Entity/Public/Base/MyTableMap.php,该类位于命名空间Model\Pomm\Entity\Public\Base中,并扩展了Pomm\Object\BaseObjectMap,它映射到postgresql的schema public中的my_table表。您当然可以使用命令行选项覆盖这些设置
$ app/console pomm:mapfile:create --database=foo --prefix-path=other/dir --prefix-namespace="Other\Namespace" --schema="other_schema" --extends="Other\\Parent" my_table
这将创建一个文件other/dir/Model/Pomm/Entity/OtherSchema/Base/MyTableMap.php,该文件包含从数据库配置中定义的foo的类Other\Namespace\Model\Pomm\Entity\OtherSchema\Base\MyTableMap,该类来自postgresql表other_schema.my_table。如果您想在bundle中存储模型文件而不是在项目目录中,这可能很有用。
当然,一个
$ app/console help pomm:mapfile:create
将帮助您:)
实际项目可能有数十(有时是数百)个表,手动生成map文件可能会很麻烦。Pomm有一个命令可以扫描Postgresql的schema,并为所有对应的Map文件生成。
$ app/console pomm:mapfile:scan
所有之前的选项也适用于此命令。
示例
在您的控制器中,使用默认数据库(第一个定义的数据库)
public function listThingsAction() { $things = $this->get('pomm') ->getDatabase() ->getConnection() ->getMapFor('Model\Pomm\Entity\NssBlog\Article') ->findAll(); ... }
您可能希望根据某些条件过滤事物
public function listActiveAndRecentThingsAction() { $things = $this->get('pomm') ->getDatabase() ->getConnection() ->getMapFor('Model\Pomm\Entity\NssBlog\Article') ->findWhere('active AND created_at > ?', array(strtotime('one month ago'))); ... }
另一个示例:从一个名为foo的数据库中调用自定义模型函数
public function myListStuffAction() { $stuff = $this->get('pomm') ->getDatabase('foo') ->getConnection() ->getMapFor('Model\Pomm\Entity\AdminUser\Group') ->myModelMethod(); ... }
Pomm 还能让您从 PostgreSQL 的良好事务机制中受益,请参阅 Pomm 的在线文档。