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/Aesislabs/php-odoo-api-client |
| ang3/php-odoo-orm | https://github.com/Aesislabs/php-odoo-orm |
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-bundleThis command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
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: falseFinally, set needed .env vars to your project:
ODOO_API_URL=
ODOO_API_DATABASE=
ODOO_API_USERNAME=
ODOO_API_PASSWORD=
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>' # optionalThe parameter default_connection is used to define the default connection to use.
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 Aesislabs\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'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
Aesislabs\Component\Odoo\Client $client:
use Aesislabs\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
Aesislabs\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 connectionFor each client, the bundle creates a public alias following this naming convention:
ang3_odoo.client.<connection_name>.
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'Get the manager of a connection easily by using dependency injection and autowiring:
namespace App;
use Aesislabs\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
Aesislabs\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 managerFor 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.
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 Aesislabs\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
- Client registry
- Beta ORM
- Registry
- Configuration
- Cache
- Record validator