Search code examples
phpxml-rpcodoo-8openerp-8

How to insert one2many values in odoo using xml-rpc


Currently I am using odoo 8.0. Actually I am creating the product using the XML-RPC API. Here the code for creating the product from xml-rpc using php.

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

The product was created successfully. Then I manually create the attribute name Color (attribute_id = 1) and the value green (value_id = 1). Next I am going to update the above varaint(Color) by the following code.

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

There is no error. It prints the updated id. But the variants are not updated in the products form view in odoo frontend. The 'attribute_line_ids' is one2many fields in product.template object. I think the syntax is incorrect for updating one2many fields from xml-rpc php. please help me. Thanks in advance.


Solution

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

    For example:

    In ODOO the product.template have One2many relation with product.attribute.line using field attribute_line_ids .

    And product.attribute.line have Many2one relation with product.template using field product_tmpl_id.

    If you want to insert a value in attribute_line_ids , then you will have to create a record in product.attribute.line.

    Please go through this code snippet :

    $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)))
                                                     ))
    

    I am damn sure that it's will help you in solving your problem.