如何使用php从many2one类型获取值

时间:2017-08-16 03:16:25

标签: php arrays openerp xml-rpc many-to-one

我想获得合作伙伴数据,其中包含信息idnamesalesperson_id,其中'salesperson_id'是多种类型(包含ID和名称)。这是我的代码:

    <?php
    // Login information
    $url = 'localhost';
    $url_auth = $url . '/xmlrp/common';
    $url_exec = $url . '/xmlrpc/object';

    $db = 'testing';
    $username = 'odoo';
    $password = 'odoo';

    // Ripcord can be cloned from https://github.com/poef/ripcord
    require_once('ripcord/ripcord.php');

    // Login
    $common = ripcord::client("$url/xmlrpc/common");
    //$common = ripcord::client($url_auth);
    $uid = $common->authenticate($db, $username, $password, array());

    //print("<p>Your current user id is '${uid}'</p>");

    $models = ripcord::client($url_exec);

    $customer = $models->execute_kw(
        $db,
        $uid,
        $password,
        'res.partner',
        'search_read', 
        array( // Search domain
            array(
                array('is_company', '=', true), // Query condition
                array('customer', '=', true)) // Query condition
            ),
        array('fields'=> array('name', 'salesperson_id'))
    );

    print("<p><strong>Found customers:</strong><br/>");
    foreach ($customer as $customers){
        print("${customers[id]}
               ${customers[name]}
               {$customers[salesperson_id][0]}

               <br/>");
        }
    print("</p>");
?>

它会显示:

Found customers:
1234 CHLOE 12
1235 DRY MART 13
1236 SEEYOU 14
1237 ED. COMP 15

解决!!

1 个答案:

答案 0 :(得分:1)

我不知道您是否要打印销售员的ID或名称。但是您的代码抛出500服务器错误的问题是IndexError。当您执行$customers[salesperson_id][1]时,它将在第一个失败,因为用户没有销售人员。因此$customers[salesperson_id]不是数组而是布尔值,并且尝试访问它的任何索引都会引发错误。

我对PHP不好,所以我只想指出答案:

  • $customers[salesperson_id][0]是销售人员的ID,$customers[salesperson_id][1]是其名称。
  • 如果客户没有销售人员,则$customers[salesperson_id]false作为价值。

所以解决方案:

  • 检查$customers[salesperson_id]是否不是false
  • 如果$customers[salesperson_id][0]不是$customers[salesperson_id][1],则
  • 打印$customers[salesperson_id]false