首页 文章

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

提问于
浏览
3

目前我正在使用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)和值green(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对象中的one2many字段 . 我认为从xml-rpc php更新one2many字段的语法不正确 . 请帮我 . 提前致谢 .

2 回答

  • 2

    One2many模型总是持有外键或我说它的关联模型中的Many2one .

    例如:

    在ODOO中, product.templateproduct.attribute.line 使用 attribute_line_ids 具有 One2many 关系 .

    product.attribute.lineproduct.templateproduct.template 使用字段 product_tmpl_id .

    如果要在 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)))
                                                     ))
    

    我很确定这会帮助你解决问题 .

  • 2

    Sharma的答案看起来是正确的(虽然我没有使用PHP),但更多解释:编写2many字段使用包含“命令”代码的三元组 . Sharma回答中的神奇“6”是使用“替换”命令 .

    (0,_ ,{' field': value}): This creates a new record and links it to this one
    (1, id,{' field': value}): This updates values on an already linked record
    (2, id,_): This unlinks and deletes a related record
    (3, id,_): This unlinks but does not delete a related record
    (4, id,_): This links an already existing record
    (5,_,_): This unlinks but does not delete all linked records
    (6,_,[ ids]): This replaces the list of linked records with the provided list
    
    The underscore symbol used above represents irrelevant values, usually filled with 0 or False.
    

    赖斯,丹尼尔 . Odoo Development Essentials Packt Publishing,2015

    PS - 我的打印副本中的第65页,索引在我的版本中已经过时了 .

相关问题