定制 pomm/pomm-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pomm/pomm-bundle

Composer 安装命令:

composer require pomm/pomm-bundle

包简介

The Symfony2 bundle for Postgresql Object Model Manager

README 文档

README

What is PommBundle (for Pomm 1.x Only)?

PommBundle makes you able to benefit from Pomm <http://pomm.coolkeums.org> and Postgres <http://postgresql.org> features from your Symfony2 <http://www.symfony.com> development.

Note:

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`.

Installation

There are several ways to install PommBundle:

The composer way

Just add the following line to your composer.json file:

{
    "minimum-stability": "dev",
    "require": {
        "pomm/pomm-bundle": "dev-master"
    }
}

And launch composer.phar install to get the bundle in the vendor directory with the autoloader set. If you are using Symfony 2.0.x, you may still be using sf2 autoloader. Update your app/autoload.php file:

$loader->registerNamespaces(array(
    'Symfony'          => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
    ...

    'Pomm'             => __DIR__.'/../vendor/pomm/pomm',
    'Pomm\\PommBundle' => __DIR__.'/../vendor/pomm/pomm-bundle',

Download the files

To use PommBundle, you can clone or download the bundle and the Pomm API in the vendor directory.

$ 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

You have now to tell Symfony2 autoloader where to find the API and the files that will be generated. Fire up your text editor and add the following lines to the app/autoload.php file:

#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__.'/..',

Setup

Let's register the PommBundle in the application kernel:

#app/AppKernel.php
        // register your bundles
        new Pomm\PommBundle\PommBundle(),

You can now define your database settings in your main configuration file. The example below uses the yaml format:

# app/config/config.yml
pomm:
    databases:
        cnct_name:
            dsn: pgsql://user:password@host:port/dbname

The cnct_name here is a name for your database. You can define several databases using different dsn or options.

#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

Now, you can configure the security layer to authenticate users:

#app/config/security.yml
security:
    providers:
        main:
            pomm:
                class: Acme\DemoBundle\Model\User
                property: email
                database: con1

Your User model must implement 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()
    {
    }
}

How to register converters

You can define global converter definitions for all databases, and/or per database:

#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

The con1 database will have the year, month and day converters. The con2 database will have the year and month converters.

How to generate Map files

A Map file is the way for Pomm to know about your tables structures. Pomm can scan the database to generate these files for you.

$ app/console pomm:mapfile:create my_table

This will create a file Model/Pomm/Entity/Public/Base/MyTableMap.php with the class MyTableMap in the namespace Model\Pomm\Entity\Public\Base extending Pomm\Object\BaseObjectMap that maps to the table my_table in the postgresql's schema public. You can of course override any of these settings using the command line options:

$ app/console pomm:mapfile:create --database=foo --prefix-path=other/dir --prefix-namespace="Other\Namespace" --schema="other_schema" --extends="Other\\Parent" my_table

This will create a other/dir/Model/Pomm/Entity/OtherSchema/Base/MyTableMap.php file owning the Other\Namespace\Model\Pomm\Entity\OtherSchema\Base\MyTableMap class from the postgres table other_schema.my_table according to the database defined as foo in the configuration. This can be useful if you want to store the model files in your bundles instead having them in the project directory.

Of course a

$ app/console help pomm:mapfile:create

will help you :)

Real life projects have dozens (sometimes hundreds) tables and it could be tiedous to generate map files one by one. Pomm has a command to scan Postgresql'schemas for tables and generate all the corresponding Map files.

$ app/console pomm:mapfile:scan

All previous options also apply for this command.

Examples

In your controllers, using the default database (the first defined):

public function listThingsAction()
{
    $things = $this->get('pomm')
        ->getDatabase()
        ->getConnection()
        ->getMapFor('Model\Pomm\Entity\NssBlog\Article')
        ->findAll();

        ...
}

You might want to filter things with some conditions:

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')));

        ...
}

Another example calling a custom model function from a database named foo:

public function myListStuffAction()
{
    $stuff = $this->get('pomm')
        ->getDatabase('foo')
        ->getConnection()
        ->getMapFor('Model\Pomm\Entity\AdminUser\Group')
        ->myModelMethod();

        ...
}

Pomm also make you benefit from Postgresql's nice transaction mechanism, see the Pomm's online documentation.

pomm/pomm-bundle 适用场景与选型建议

pomm/pomm-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.29k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2012 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「orm」 「symfony」 「postgresql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pomm/pomm-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pomm/pomm-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 pomm/pomm-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 30.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 32
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 5
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-10-25