Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Odoo Orm Laravel Package

ang3/php-odoo-orm

View on GitHub
Deep Wiki
Context7
## Getting Started

### Minimal Setup
1. **Installation**
   ```bash
   composer require ang3/php-odoo-orm

Add the autoloader to your project (Composer handles this automatically).

  1. First Connection

    use Ang3\Odoo\OdooConnection;
    
    $connection = new OdooConnection(
        'http://your-odoo-instance.com',
        'database_name',
        'username',
        'password'
    );
    
  2. First Query (Fetching Records)

    $model = $connection->getModel('res.partner'); // Odoo model name
    $partners = $model->findAll(); // Fetch all records
    
  3. First Write Operation

    $partner = $model->create([
        'name' => 'John Doe',
        'email' => 'john@example.com'
    ]);
    
  4. Searching Records (Fixed in v0.1.9)

    // Use searchAll() for advanced searches (now fully functional in v0.1.9)
    $results = $model->searchAll('John', ['name', 'email']);
    

    Note: The searchAll() method was previously fixed in v0.1.9 to properly handle multi-field searches with Odoo's XML-RPC API.


Implementation Patterns

Common Workflows

1. CRUD Operations

// Create
$record = $model->create(['field1' => 'value1']);

// Read
$record = $model->find($id);
$records = $model->findAll(['filter' => ['active' => true]]);

// Update
$record->name = 'Updated Name';
$record->save();

// Delete
$model->delete($id);

2. Query Filtering

// Basic filter
$activePartners = $model->findAll([
    'domain' => [['active', '=', true]]
]);

// Search with fields (now reliable in v0.1.9)
$results = $model->searchAll('John', ['name', 'email']); // Search across multiple fields

3. Relationships (One2Many, Many2One)

// Load related records (e.g., orders for a partner)
$partner = $model->find($id);
$orders = $partner->orders->findAll(); // Assumes 'orders' is a defined relation

4. Batching Large Datasets

$batchSize = 100;
$offset = 0;
do {
    $records = $model->findAll([
        'limit' => $batchSize,
        'offset' => $offset
    ]);
    // Process records...
    $offset += $batchSize;
} while (count($records) > 0);

5. Custom XML-RPC Methods

// Call a custom Odoo method (e.g., `action_confirm`)
$result = $model->execute('action_confirm', [[$id]]);

Integration Tips

Laravel Service Provider

Bind the connection to Laravel’s container for dependency injection:

// config/app.php
'bindings' => [
    Ang3\Odoo\OdooConnection::class => function ($app) {
        return new OdooConnection(
            config('odoo.url'),
            config('odoo.database'),
            config('odoo.username'),
            config('odoo.password')
        );
    },
];

Eloquent-like Facades

Create a facade for cleaner syntax:

// app/Facades/Odoo.php
namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Odoo extends Facade {
    protected static function getFacadeAccessor() {
        return 'odoo.connection';
    }
}

Usage:

$partners = Odoo::model('res.partner')->findAll();
$searchResults = Odoo::model('res.partner')->searchAll('John', ['name', 'email']);

Event Listeners

Hook into Odoo events (e.g., after record creation) using Laravel’s event system:

// Listen for Odoo model events
$model->on('created', function ($record) {
    // Trigger Laravel event or logic
});

Caching Responses

Cache frequent queries to reduce API calls:

$cacheKey = "odoo_partners_active";
$partners = Cache::remember($cacheKey, now()->addHours(1), function () use ($model) {
    return $model->findAll(['domain' => [['active', '=', true]]]);
});

Gotchas and Tips

Pitfalls

  1. Model Naming

    • Odoo uses dot notation for technical names (e.g., account.move).
    • The package expects underscores (account_move) in method calls. Verify case sensitivity:
      // Correct
      $model = $connection->getModel('account_move');
      // Incorrect (may fail silently)
      $model = $connection->getModel('AccountMove');
      
  2. Field Names vs. Database Fields

  3. Rate Limiting

    • Odoo’s XML-RPC API may throttle requests. Implement retries:
      try {
          $result = $model->find($id);
      } catch (\Exception $e) {
          if ($e->getCode() === 400) { // Example: Bad request
              retry($attempts++, function () use ($model, $id) {
                  return $model->find($id);
              }, 1000); // Retry after 1s
          }
      }
      
  4. Authentication Failures

    • Silent failures on wrong credentials. Enable debug mode:
      $connection = new OdooConnection(..., ..., ..., ..., true); // Enable debug
      
      Check logs for XML-RPC errors.
  5. Large Payloads

    • Creating/updating records with many fields may hit Odoo’s payload limits.
    • Split operations into batches or use write() for partial updates:
      $model->write([$id], ['field1' => 'value1']); // Update single field
      
  6. Search Method Fix (v0.1.9)

    • The searchAll() method was fixed in v0.1.9 to properly handle multi-field searches.
    • Avoid using older methods like search() for multi-field searches, as they may not work as expected.
    • Example:
      // Correct (v0.1.9+)
      $results = $model->searchAll('John', ['name', 'email']);
      
      // Avoid (may not work as intended)
      $results = $model->search('John', ['name']);
      

Debugging Tips

  1. Enable Verbose Logging

    $connection->setDebug(true);
    // Check logs for raw XML-RPC requests/responses.
    
  2. Inspect Raw XML-RPC Calls Use a tool like Postman or SoapUI to manually test endpoints before debugging the PHP code.

  3. Handle Odoo Exceptions Catch Ang3\Odoo\Exception\OdooException for Odoo-specific errors:

    try {
        $model->create([...]);
    } catch (\Ang3\Odoo\Exception\OdooException $e) {
        // Parse Odoo error messages (often in $e->getMessage())
    }
    
  4. Check Odoo Server Logs

    • Odoo logs XML-RPC errors to /var/log/odoo/odoo-server.log (default path).
    • Look for xmlrpc or request keywords.
  5. Search Method Debugging (v0.1.9)

    • If searchAll() behaves unexpectedly, verify:
      • The search term is correctly formatted (e.g., 'John' for partial matches).
      • The fields passed are valid technical field names for the model.
    • Example:
      // Ensure 'email' is a valid field for the model
      $results = $model->searchAll('John', ['name', 'email']);
      

Extension Points

  1. Custom Model Classes Extend Ang3\Odoo\Model to add domain-specific logic:
    class CustomPartnerModel extends \Ang3\Odoo\Model {
        public function getActivePartnersWithEmail() {
            return $this->searchAll('', ['name', 'email'], [
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor