如何通过xmlrpc将many2many发送到openerp

时间:2013-09-17 13:45:54

标签: php openerp xml-rpc

我正在开发一个涉及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”,但该库不支持此功能。

1 个答案:

答案 0 :(得分:1)

好吧,我刚刚使用XMLRPC库来解决它。它不是你的确切库,但我想也许我在这里分享的内容可以提供帮助。以下是一些示例代码:

// 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);

基本上,您必须拥有[[6,0,[id1,id2,id3,etc.]]]的结构。使用XMLRPC,所有值都必须在xmlrpcval对象中正确打包,最终很难读取。

在我构建的系统中,我已将所有这些打包并重构为一些辅助函数,因此不太了解正在发生的事情。不过,这对屁股很痛苦!