0
votes

I'm working on a project involving XML-RPC in PHP to connect to OpenERP 6.1.1

I need to create a function that will update a many2many relation, precisely the supplier_taxes_rel of product.template object.

In python, we would do "supplier_taxes_id = '[(6,0,[38, 40])]'".

I am currently using "openerplib.php" from "https://github.com/b3ni/openerplib", but the library doesn't support this feature.

1

1 Answers

1
votes

Well, I just figured it out using the XMLRPC library. It's not the exact library as you, but I think maybe what I share here can help. Here is some sample code:

// set up the overall message parameters
$msg = new xmlrpcmsg('execute');
$msg->addParam(new xmlrpcval(self::DBNAME, "string"));
$msg->addParam(new xmlrpcval($uid, "int"));
$msg->addParam(new xmlrpcval(self::PASSWORD, "string"));
$msg->addParam(new xmlrpcval($someObject->getOpenERPName(), "string"));
$msg->addParam(new xmlrpcval("write", "string"));
$msg->addParam(new xmlrpcval($someObject->getId(),"int"));

// prep the many2many data
// yes, this is ridiculous, and hard to understand!
$m2m = new xmlrpcval(
    array(
        new xmlrpcval(
            array(
                new xmlrpcval(6,"int"),
                new xmlrpcval(0,"int"),
                new xmlrpcval(
                    array(
                        new xmlrpcval(35,"int"), // id of the object at the other side of the relation
                        new xmlrpcval(52,"int"), // id of the object at the other side of the relation
                    ),"array")
            ), "array")
    ),"array");

// package all the data
$data = array(
    "field1"=>new xmlrpcval($someObject->getField1(),"string"),
    "field2"=>new xmlrpcval($someObject->getField2(),"int"),
    "field3"=>$m2m,
);

// add the data to the message  
$msg->addParam(new xmlrpcval($data,"struct"));

// send the message
$client_object = new xmlrpc_client(
     "http://".self::HOST.":".self::PORT."/xmlrpc/object"
);
$resp = $client_object->send($msg);

Basically, you have to have a structure that is [[6,0,[id1,id2,id3,etc.]]]. With XMLRPC, the values all have to be properly packed in xmlrpcval objects and it ends up being very hard to read.

In the system I'm building, I've packaged all this up and refactored it into some helper functions so that it's not quite so hard to understand what is going on. Still, it's a pain in the butt!