定制 ang3/odoo-bundle 二次开发

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

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

ang3/odoo-bundle

Composer 安装命令:

composer require ang3/odoo-bundle

包简介

Symfony bundle to manage your Odoo databases.

README 文档

README

Build Status Latest Stable Version Latest Unstable Version Total Downloads

This bundle is a Symfony integration of packages ang3/php-odoo-api-client and ang3/php-odoo-orm.

Main features:

  • Client registry
  • Object relational mapping (ORM)
  • Debugging commands
  • Record validator

Documentation of both packages:

Package Documentation
ang3/php-odoo-api-client https://github.com/Ang3/php-odoo-api-client
ang3/php-odoo-orm https://github.com/Ang3/php-odoo-orm

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require ang3/odoo-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Configure your app

Create the file config/packages/ang3_odoo.yaml and paste the configuration below:

# app/config/config.yml or config/packages/ang3_odoo.yaml
ang3_odoo:
  default_connection: default
  default_logger: '<logger_service_name>' # Instance of \Psr\Log\LoggerInterface (optional)
  # If set, the default logger is used if a connection hasn't one
  connections:
    default:
      url: '%env(resolve:ODOO_API_URL)%'
      database: '%env(resolve:ODOO_API_DATABASE)%'
      user: '%env(resolve:ODOO_API_USERNAME)%'
      password: '%env(resolve:ODOO_API_PASSWORD)%'
      logger: '<logger_service_name>' # Instance of \Psr\Log\LoggerInterface (optional)
  orm:
    enabled: false

Finally, set needed .env vars to your project:

ODOO_API_URL=
ODOO_API_DATABASE=
ODOO_API_USERNAME=
ODOO_API_PASSWORD=

Work with multiple connections (optional)

You can add more connection under section ang3_odoo.connections. Here is an example for another connection:

# app/config/config.yml or config/packages/ang3_odoo.yaml
ang3_odoo:
  # ...
  connections:
    default:
      # ...
    other_connection_name:
      url: '...'
      database: '...'
      user: '...'
      password: '...'
      logger: '<logger_service_name>' # optional

The parameter default_connection is used to define the default connection to use.

Usage

Registry

If you want to work with all your configured clients, then you may want to get the registry. It stores all configured clients by connection name. You can get it by dependency injection:

use Ang3\Bundle\OdooBundle\ClientRegistry;

class MyService
{
    private $clientRegistry;

    public function __construct(ClientRegistry $clientRegistry)
    {
        $this->clientRegistry = $clientRegistry;
    }
}

The registry contains three useful methods:

  • public function set(string $connectionName, Client $client): self Set a client by connection name.
  • public function get(string $connectionName): Client Get the client of a connection. A \LogicException is thrown if the connection was not found.
  • public function has(string $connectionName): bool Check if a connection exists by name.

If you don't use autowiring, you must pass the service as argument of your service:

# app/config/services.yml or config/services.yaml
# ...
MyClass:
    arguments:
        $clientRegistry: '@ang3_odoo.client_registry'

Clients

It could be useful to get a client directly without working with the registry.

For example, the get the default client by autowiring, use the argument Ang3\Component\Odoo\Client $client:

use Ang3\Component\Odoo\Client;

class MyService
{
    private $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }
}

If the connection name is foo_bar, then the autowired argument is Ang3\Component\Odoo\Client $fooBarClient.

  • Run the command php bin/console debug:autowiring Client to get the list of autowired clients.

Of course if you don't use autowiring, you must pass the service as argument of your service:

# app/config/services.yml or config/services.yaml
# ...
App\MyService:
    arguments:
        $client: '@ang3_odoo.client.<connection_name>' # Or '@ang3_odoo.client' for the default connection

For each client, the bundle creates a public alias following this naming convention: ang3_odoo.client.<connection_name>.

ORM

To enable ORM features, you must edit the file config/packages/ang3_odoo.yaml to configure it:

ang3_odoo:
  # ...
  orm:
    enabled: true # Do not forget to enable the ORM
    managers:
      default: # connection name to manage
        paths: # List of directories where your Odoo objects are stored
          - '%kernel_project_dir%/src/Odoo/Entity'

Usage

Get the manager of a connection easily by using dependency injection and autowiring:

namespace App;

use Ang3\Component\Odoo\ORM\ObjectManager;

class MyService
{
    /**
     * @var  ObjectManager
    */
    private $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }
}

If the connection name is foo_bar, then the autowired argument is Ang3\Component\Odoo\ORM\ObjectManager $fooBarObjectManager. By default, the default manager is autowired.

  • Run the command php bin/console debug:autowiring ObjectManager to get the list of autowired managers.

Of course if you don't use autowiring, you must pass the service as argument of your service:

# app/config/services.yml or config/services.yaml
# ...
App\MyService:
    arguments:
        $objectManager: '@ang3_odoo.orm.object_manager.<connection_name>' # Or '@ang3_odoo.orm.object_manager' for the default manager

For each manager, the bundle creates a public alias following this naming convention: ang3_odoo.orm.object_manager.<connection_name>.

Please read the documentation of the ORM package ang3/php-odoo-orm to know more information about the object manager.

Validator

This bundle provides a useful validator according to the package symfony/validator to validate a record by ID, domains and/or connection. It resides to a basic annotation.

Here is an example of an object storing the ID of a company and invoice:

use Ang3\Bundle\OdooBundle\Validator\Constraints as Odoo;

class MyEntity
{
    /**
     * @var int
     *
     * @Odoo\Record("res.company")
     * ...
     */
    private $companyId;

    /**
     * @var int
     *
     * @Odoo\Record(model="account.move", domains="expr.eq('company_id.id', this.companyId)", connection="default")
     * ...
     */
    private $invoiceId;
}

Here is the list of all options you can pass to the annotation:

  • model (required string) The model name of the record.
  • domains (string) An expression which evaluation must returns valid client criteria.
  • connection (string) the name of the connection to use
    • By default the default connection is used.
  • typeErrorMessage (string) The error message if the value is not a positive integer
    • By default the message is: This value must be a positive integer.
  • notFoundMessage (string) The message if the record was not found
    • By default the message is: The record of ID {{ model_id }} from "{{ model_name }}" was not found.

As you can see, the validator uses both symfony/expression-language and the expression builder provided with the client. By this way, you can filter allowed records easily.

Here are the variable passed to the evaluated expression:

  • expr the expression builder
  • this the object that the property/getter belongs to
  • user the user of the request Symfony\Component\Security\Core\User\UserInterface|null

Upgrades & updates

v0.1.0 (beta-release)

  • Client registry
  • Beta ORM
    • Registry
    • Configuration
    • Cache
  • Record validator

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.89k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-04