DarkaOnLine / Ripcord

XML RPC client and server around PHP's xmlrpc library
33 stars 21 forks source link

using Ripcord in Laravel #15

Open cgrisar opened 6 years ago

cgrisar commented 6 years ago

Hi,

so I got Ripcord to load and all. So for so good.

However, I run into trouble trying to use it.

I didn't get very far:

public function home() {
        $config = array(
            'url' => 'https://db.odoo.com/xmlrpc/2',
            'db' => 'db',
            'user' => 'user',
            'password' => 'password'
        );

        $common = new Ripcord ( $config );

Connection seems to work.

However, I don't know how to make good use of it. For example

$common->execute('res.partner');

Throws me back an error, saying method not found.

so, how do I use the execute_kw() method?

Thanks,

Charles

kurodaSensei commented 5 years ago

Hello @cgrisar, I apologize in advance for my poor English and also because the answer has come to you quite late, I also spent some time hitting my head with this.

The solution is very easy, I explain:

The first thing you should do is extend / instantiate the Ripcord class in your class, in my case I am using a Laravel Controller to test

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Ripcord\Providers\Laravel\Ripcord;

class TestController extends Ripcord 

Then I create a public function to test, within the function I create a new instance of Ripcord

$common = new Ripcord;

Being extended from the Ripcord class, when creating a new object, I can access the attributes of Ripcord through the common object, Now you must understand how the Ripcord class works.

public function __construct($config = [])
    {
        $this->url = isset($config['url']) ? $config['url'] : config('ripcord.url');
        $this->db = isset($config['db']) ? $config['db'] : config('ripcord.db');
        $this->username = isset($config['user']) ? $config['user'] : config('ripcord.user');
        $this->password = isset($config['password']) ? $config['password'] : config('ripcord.password');

        $this->connect();
    }

    /**
     * Create connection.
     */
    public function connect()
    {
        $common = RipcordBase::client("$this->url/common");
        $this->uid = $common->authenticate($this->db, $this->username, $this->password, []);
        $this->client = RipcordBase::client("$this->url/object");
    }

When creating the common object, it automatically connects to odoo and obtains the uid and the model (which you will later use to perform your operations)

Then, knowing this, you can run any method of the odoo API as follows:

$result = $common->client->execute_kw(
            $common->db,
            $common->uid,
            $common->password,
            'crm.lead',
            'search',
            array( array() ), array() );
return $result;

In this piece of code I call the execute_kw function from the client (model) returned by the main instance of Ripcord, passing the necessary parameters db, uid and password also obtained from the Ripcord class, this call is to obtain a list of all id's of the leads registered in the odoo crm module.

I hope it helps.