0

我正在开发一个涉及 PHP 中的 XML-RPC 以连接到 OpenERP 6.1.1 的项目

我需要创建一个函数来更新many2many 关系,即product.template 对象的supplier_taxes_rel。

在 python 中,我们会执行“supplier_taxes_id = '[(6,0,[38, 40])]'”。

我目前正在使用“ https://github.com/b3ni/openerplib ”中的“openerplib.php” ,但该库不支持此功能。

4

1 回答 1

1

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!

于 2014-01-09T06:44:13.610 回答