Search by Many2One Field odoo PHP XMLRPC - php

i'm using this class for accessing odoo through PHP XMLRPC.
Everything works fine almost. I can't search many2one fields just by placing the id of the referenced record. I guess there should be a special way to code the many2one id in the search.
EG: searching in product.supplierinfo by the product_tmpl_id using this code i get emtpy array returned:
$rpc->searchread(array(array("product_tmpl_id","=","3673")),"product.supplierinfo");
Searching for the record by id i get this result:
$rpc->read(array(1),"", "product.supplierinfo");
Array
(
[0] => Array
(
[create_uid] => Array
(
[0] => xxxxx
[1] => xxxxx xxxxx
)
[product_code] =>
[create_date] => 2016-06-22 11:08:00
[name] => Array
(
[0] => 1438
[1] => Provider one
)
[product_uom] => Array
(
[0] => 1
[1] => Unit(s)
)
[sequence] => 1
[product_name] =>
[__last_update] => 2016-06-22 11:42:28
[company_id] => Array
(
[0] => 1
[1] => Company Name
)
[write_uid] => Array
(
[0] => xxxx
[1] => xxxxxx xxxxx
)
[delay] => 1
[write_date] => 2016-06-22 11:42:28
[pricelist_ids] => Array
(
)
[display_name] => Provider One
[min_qty] => 0
[qty] => 0
[product_tmpl_id] => Array
(
[0] => 3673
[1] => Product Name
)
[id] => 1
)
)
How should i code the id of a many2one field?
Any help will be appreciated.

Finally i had the solution. As #czoellner suggested was a problem was the type of the ID was a string and must be an integer. So, this code worked fine.
$rpc->searchread(array(array("product_tmpl_id","=",3673)),"product.supplierinfo");
On the other hand must consider the issue between the id of the product.product and the id of the product.template because they are differents. The table product.supplierinfo uses the id of product.template instead product.product. That causes for searching through the table produc.supplierinfo is necessary to find first the product_tmpl_id of the product to refer to the product. So for finding a all the providers of a product:
#search it by id
$prod_odoo = $rpc->read(array(1),"product.product",array());
#every search returns a 2 dimension array
$prod_suppliers = $rpc->searchread(array(array("product_tmpl_id","=",(int)$prod_odoo[0]["product_tmpl_id"][0])),"product.supplierinfo");
Hope this helps.

Related

Nested JSON store in MySQL using PHP

I would like to ask for your help with the following.
I am getting similar nested json as mentioned below in PHP. I would like to store the data in MySQL db, but I am facing problem with the variable information.
My problem is that the data are not consistent. As you can see in json below:
[6] => Array
(
[pflds] => Array
(
[1] => Array
(
[id] => 1
[n] => registration_plate
[v] => xxxxxx
)
[2] => Array
(
[id] => 2
[n] => vehicle_type
[v] => xxxxx
)
[3] => Array
(
[id] => 3
[n] => brand
[v] => xxxx
)
)
[pfldsmax] => 0
)
[7] => Array
(
[pflds] => Array
(
[1] => Array
(
[id] => 1
[n] => vehicle_type
[v] => Трактор
)
[2] => Array
(
[id] => 2
[n] => registration_plate
[v] => xxxxx
)
[3] => Array
(
[id] => 3
[n] => brand
[v] => John Deere
)
)
[pfldsmax] => 0
)
Once the registration plate is
Array -> [6] -> [1] -> [n] registration plate
and then
Array -> [7] -> [2] -> [n] registration plate
So I cannot easily say "insert into" because the column's position varies.
The field [n] has the same name at any time, the [id] can vary though.
I am not able to think of any conditions to use. But I would like to focus on the field [n] which will say in which column the value will be stored.
Any suggestions or any other approach to go?
I'm new to this field. Thx for the help.
You can use foreach loop a several times like:
foreach($ar as $ind1=>$set) { // ind1 = 6 or 7
foreach($set as $key=>$subar){ // key = 'pflds' or 'pfldsmax'
if (is_array($subar)){ // if subar is an array
foreach($subar as $item){
if ($item['n'] === 'registration_plate'){
// code for insert into DB
echo $item['id'].PHP_EOL;
}
}
}
}
}
Demo

Array parsing using array value

i have below array,and i have amenities id = 50,i need to show amenities name like 'Express check-out' using amenities id = 50 from this array using php.
Array
(
[amenities] => Array
(
[0] => Array
(
[id] => 0
[name] => Cash machine
[key] => CASHMACHINE
)
[1] => Array
(
[id] => 42
[name] => Express check-in
[key] => EXPRESSCHECKINSERVICE
)
[2] => Array
(
[id] => 50
[name] => Express check-out
[key] => EXPRESSCHECKOUTSERVICE
)
[5] => Array
(
[id] => 3
[name] => Wi-Fi
[key] => WIFISERVICE
)
)
)
There are many ways that your problem can be solved. Easy way can be as follow
function getAmenities($array,$id){
foreach($array['amenities'] as $tmp_arr)
if($tmp_arr['id']==$id)
return $tmp_arr['name'];
}
echo getAmenities($array,50);
I have not checked result but should work fine. Please let me know if this works for you
How do u create this Array?
Can't u just use the id as the key when u create it, like:
$key = $array2['id'];
$array['amenities'][$key] = $array2;

How to show specific value from array with mustache?

I have an array like:
Array
(
[0] => Array
(
[title] => Title 1
[value] => Value 1
[id] => 1428735262
)
[1] => Array
(
[title] => Title 2
[value] => Value 2
[id] => 2428735262
)
[2] => Array
(
[title] => Title 3
[value] => Value 3
[id] => 3428735262
)
)
With mustache I am able to iterate overall of them using:
{{#values}}
<h1>{{title}}</h1>
{{{value}}}
{{/values}}
But what if I only want to show specific value, for example only second element of the array (one with Title 2)?
I tried like:
{{#values}}
{{#2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/2428735262}}
{{/values}}
and:
{{#values.2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/values.2428735262}}
But none worked. Does anyone know how to get specific item from array ?
PS. I am using PHP version of mustache. Thanks
You can use this:
{{values.1.value}}
Read more at:
https://github.com/bobthecow/mustache.php/wiki/Variable-Resolution

Yii createCommand() return data set in different array format

I'm a beginner in this framework. Though I have gone over the basics, there is one thing which is troubling me. As of now, I'm using
$group_sql = "SELECT uid FROM {$table}";
$group_users = Yii::app()->db->createCommand($group_sql)->queryAll();
print_r($group_users);
results in
Array
(
[0] => Array
(
[uid] => 2
)
[1] => Array
(
[uid] => 3
)
[2] => Array
(
[uid] => 4
)
[3] => Array
(
[uid] => 5
)
)
But I'd like to change the format in which the data is returned. What I'm looking for is something like
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
OR
Array
(
[uid] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
)
I'm aware that I can go through the documentation and get my answer, but due to time constraints, I'm taking the liberty to shamelessly ask this over here.
Thanks in advance.
Use queryColumn() method instead of queryAll()

How to get nested array from MySQL select with joins

I have the models User, Post, Comment and Tag.
User creates Post.
Posts can have multiple Comment and Tag.
Each model has it's own table, so there are tables 'posts', 'comments' and 'tags'. Comments have a foreign key called 'post_id', while Tags have a many_to_many relation table called 'post_tags', in which there are two fields: 'post_id' and 'tag_id'.
I want to get a nested array like below:
Which MySQL queries should I run?
I suppose I need to alter the result with PHP to get my nested array. How?
Thanks a lot for your help :-)
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => First article
[content] => aaa
[created] => 2008-05-18 00:00:00
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[post_id] => 1
[author] => Daniel
[email] => dan#example.com
[website] => http://example.com
[comment] => First comment
[created] => 2008-05-18 00:00:00
)
[1] => Array
(
[id] => 2
[post_id] => 1
[author] => Sam
[email] => sam#example.net
[website] => http://example.net
[comment] => Second comment
[created] => 2008-05-18 00:00:00
)
)
[Tag] => Array
(
[0] => Array
(
[id] => 1
[name] => Awesome
)
[1] => Array
(
[id] => 2
[name] => Baking
)
)
)
[1] => Array
(
[Post] => Array
(...
You better of doing 3 queries.
first fetch post (, and left join the user if you need it), and store them like:
$list[$row['post_id']]['Post'] = $row;
then fetch all post-comments and store them as
$list[$row['post_id']]['Comment'][$row['comment_id']] = $row;
then fetch all post-tags
$list[$row['post_id']]['Tags'][$row['tag_id']] = $row;
that far more effective than trying to use a single query,
as a single query going to end up sending the same data multiple times

Categories