ang3/odoo-bundle
Composer 安装命令:
composer require ang3/odoo-bundle
包简介
Symfony bundle to manage your Odoo databases.
README 文档
README
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): selfSet a client by connection name.public function get(string $connectionName): ClientGet the client of a connection. A\LogicExceptionis thrown if the connection was not found.public function has(string $connectionName): boolCheck 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 Clientto 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 ObjectManagerto 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
defaultconnection is used.
- By default the
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.
- By default the message is:
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.
- By default the message is:
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:
exprthe expression builderthisthe object that the property/getter belongs touserthe user of the requestSymfony\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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ang3/odoo-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
This bundle provides tools to build persistence-agnostic storage layer.
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
统计信息
- 总下载量: 4.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-04