微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-如何使用xml-rpc在odoo中插入one2many值

目前,我正在使用odoo 8.0.实际上,我正在使用XML-RPC API创建产品.这里是使用PHP从xml-rpc创建产品的代码.

$url = "http://localhost:8069";
$db = "xmlcreate";
$username = "admin";
$password = "admin";
require_once('ripcord-master/ripcord.PHP');
$common = ripcord::client("$url/xmlrpc/2/common");
$uid = $common->authenticate($db, $username, $password, array());
$models = ripcord::client("$url/xmlrpc/2/object");

$product = array('name' => 'Sample',
                 'type' => 'product',
                 'list_price' => 4.6,
                 'standard_price' => 3.25
           );
$product_id = $models->execute_kw($db, $uid, $password,  'product.template','create',array($product));

产品创建成功.然后,我手动创建属性名称Color(attribute_id = 1)和值绿色(value_id = 1).接下来,我将通过以下代码更新上述varaint(Color).

$attributes = array();
$attributes[] = 0;
$attributes[] = 0;
$attributes['attribute_id'] = 1; // attribute is color (color -> 1)
$attributes['values_id'] = array(1); // attribute value is green(green -> 1) 

$existing_prodid = 1;
$up_attr_id = $models->execute_kw($db, $uid, $password,'product.template','write',array($existing_prodid, array('attribute_line_ids' => $attributes)));
print_r($up_attr_id);

没有错误.它将打印更新的ID.但是这些变体不会在odoo前端的产品表单视图中更新. “ attribute_line_ids”是product.template对象中的一个字段.我认为从xml-rpc PHP更新one2many字段的语法不正确.请帮我.提前致谢.

解决方法:

One2many model always hold the foreign key or i say Many2one of
it’s associative model .

例如:

odoO中,product.template使用字段attribute_line_ids与product.attribute.line具有One2many关系.

并且product.attribute.line使用product_tmpl_id字段与product.template具有Many2one关系.

如果要在attribute_line_ids中插入一个值,则必须在product.attribute.line中创建一条记录.

请仔细阅读以下代码段:

$existing_prodid = 59;
$existing_attribute_id = 2;
$existing_value_id = 4;
$product_attribute_line = $models->execute($db, $uid, $password,
                                       'product.attribute.line','create',
                                        array('product_tmpl_id' => $existing_prodid;,
                                            'attribute_id'=>$existing_attribute_id,
                                            'value_ids'=>array(array(6,0,array($existing_value_id)))
                                                 ))

我很确定这将帮助您解决问题.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐