balaraman
balaraman

Reputation: 413

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.

Upvotes: 3

Views: 3850

Answers (2)

joe
joe

Reputation: 332

Sharma's answer looks to be correct (although I am not using PHP), but a little more explanation: writing the 2many fields uses a triple containing a "command" code. The magic "6" in Sharma's answer is using the "replace" command.

(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.

Reis, Daniel. Odoo Development Essentials Packt Publishing, 2015

PS - page 65 in my print copy, the index is way off in the edition I have.

Upvotes: 2

Prakash Kumar
Prakash Kumar

Reputation: 2594

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.

Upvotes: 2

Related Questions