【问题标题】:Odoo rpc query with join in C#在 C# 中使用连接的 Odoo rpc 查询
【发布时间】:2019-11-07 16:24:13
【问题描述】:

我有这种类型的代码可以从OdooRpc中进行选择:

XmlRpcClient client = new XmlRpcClient();
client.Url = LocalApplication.Url;
client.Path = "common";

// LOGIN BG
XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
requestLogin.AddParams(LocalApplication.Db, LocalApplication.User, LocalApplication.Pass, XmlRpcParameter.EmptyStruct());

XmlRpcResponse responseLogin = client.Execute(requestLogin);

// READ
client.Path = "object";

// var x = client.Execute("select * from res_partner");
XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
                        LocalApplication.Pass, "res.partner", "search_read",
                        XmlRpcParameter.AsArray(
                           XmlRpcParameter.AsArray(
                              XmlRpcParameter.AsArray("is_agent", "=", "True".ToLower()))),
                        XmlRpcParameter.AsStruct(
                           XmlRpcParameter.AsMember("fields",
                              XmlRpcParameter.AsArray("name", "id", "phone", "email"))));

// SAVE
XmlRpcResponse responseSearch = client.Execute(requestSearch);

var agents = (responseSearch.GetObject() as List<object>);
Taswiq.Bussiness.Person p = new Person(LocalApplication.ConnectionString);

if (agents != null)
{
    foreach (var a in agents)
    {
        Dictionary<string, object> agent = a as Dictionary<string, object>;
        p.Name = agent["name"].ToString() == "False" ? "ND" : agent["name"].ToString();
        p.Marca = Convert.ToInt32(agent["id"]);
        p.Type = 1;
        p.Phone = agent["phone"].ToString() == "False" ? "ND" : agent["phone"].ToString();
        p.Email = agent["email"].ToString() == "False" ? "ND" : agent["email"].ToString();

        MessageStruct result = p.Save();

        if (result.HasErrors)
        {
            return result.Message;
        }
    }

    return "Success";
}

一切正常,但现在我需要从这两个表中进行一些连接,如下所示:

Select * 
from product_template as d 
inner join product_product as r on r.product_tmpl_id = d.id;

除了 Python 代码,我什么也找不到。

谁能帮我解决这个问题?

【问题讨论】:

    标签: c# odoo rpc pgadmin


    【解决方案1】:

    我想我找到了解决办法。我不知道是否是最好的方法,但就我而言,它解决了问题。

    我已经为每个需要加入的表列出了相同的列表:

    public List<object> lstStock = new List<object>();
    
     public void PopulateStockList()
        {
            XmlRpcClient stock = new XmlRpcClient();
            stock.Url = LocalApplication.Url;
            stock.Path = "common";
    
            //LOGIN BG
            XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
            requestLogin.AddParams(LocalApplication.Db, LocalApplication.User, 
    LocalApplication.Pass,
                XmlRpcParameter.EmptyStruct());
    
            XmlRpcResponse responseLogin = stock.Execute(requestLogin);
    
            //READ
    
            stock.Path = "object";
    
            XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");
            requestSearch.AddParams(LocalApplication.Db, responseLogin.GetInt(),
                LocalApplication.Pass, "stock.quant", "search_read",
                XmlRpcParameter.AsArray(
                    XmlRpcParameter.AsArray(
                        XmlRpcParameter.AsArray("id", ">", 0)
                    )
                ),
                XmlRpcParameter.AsStruct(
                    XmlRpcParameter.AsMember("fields",
                        XmlRpcParameter.AsArray("product_id", "quantity", "in_date", 
    "owner_id"))
                )
            );
            XmlRpcResponse responseSearch = stock.Execute(requestSearch);
            lstStock = (responseSearch.GetObject() as List<object>);
        }
    

    我需要加入的每个表都采用相同的方式。 最后,在foreach 中,我创建了我需要填充sql 表的连接

     public string DownloadRpcStock()
        {
            try
            {
                PopulateProdTemplate();
                PopulateStockList();
                PopulateProductList();
    
                Stock s = new Stock(LocalApplication.ConnectionString);
    
                foreach (var st in lstStock)
                {
                    Dictionary<string, object> stc = st as Dictionary<string, object>;
                    var productId = stc["product_id"] as List<object>;
                    if (productId != null)
                        s.IdProduct = Convert.ToInt32(productId[0]); //Din lista care vine pe product_id imi iau valoarea dorita
                    s.IdStorage = 1;
                    s.StockQuantity = Convert.ToDouble(stc["quantity"]);
                    s.Rest = Convert.ToDouble(stc["quantity"]);
    
                    var prod = lstProduct.FirstOrDefault(
                        k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) == Convert.ToInt32((stc["product_id"] as List<object>)[0])) as Dictionary<string, object>;
    
                    var prodTemplate =
                        lstProdTemplate.FirstOrDefault(
                            k => Convert.ToInt32((k as Dictionary<string, object>)["id"]) ==
                                 Convert.ToInt32((prod["product_tmpl_id"] as List<object>)[0])) as Dictionary<string, object>;
                    if (prodTemplate != null)
                        s.Price = Convert.ToDouble(prodTemplate["list_price"]);
                    s.Batch = "NoBatch"; // Provizoriu
                    s.Date = Convert.ToDateTime(stc["in_date"]);
                    s.ExpireDate = Convert.ToDateTime(stc["in_date"]); // Provizoriu
                    s.IdCustomerSuplier = Convert.ToInt32(stc["owner_id"]);
                    s.Id = 0; //if (Id == 0) act = insert
                    MessageStruct result = s.Save();
                    if (result.HasErrors)
                    {
                        return result.Message;
                    }
                }
    
                return "Success";
            }
    
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    

    我在互联网上发现了很多这样的问题,但没有解决方案。 我希望我的解决方案将来能对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 2011-07-05
      • 2021-03-02
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多