This bundle is a Symfony integration of packages ang3/php-odoo-api-client and ang3/php-odoo-orm.
Main features:
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 |
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.
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=
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.
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'
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.
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>.
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 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.
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.
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
default connection is used.typeErrorMessage (string) The error message if the value is not a positive integer
This value must be a positive integer.notFoundMessage (string) The message if the record was not found
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 builderthis the object that the property/getter belongs touser the user of the request Symfony\Component\Security\Core\User\UserInterface|nullHow can I help you explore Laravel packages today?