Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Xmlrpc,php 操作 OE 的方法 --- 搜索、读取、创建、更新



  • php 操作 OE 的方法

    phpxmlrpc 版本:v2.2.2([url=http://phpxmlrpc.sourceforge.net/]下载[/url])
    openerp 版本:v7.0-20130103-002220-1
    LNMP 版本:v0.9

    * 搜索
    * 读取
    * 创建
    * 更新。

    如果报 "Call to a member function scalarval() on a non-object" 错,请看看是不是 fsockopen 函数被禁用。
    * vi /usr/local/php/etc/php.ini
    * 搜索 disable_functions, 删除里面的 fsockopen

    还有个问题,创建和更新的方法,不能用 $GLOBALS['xmlrpc_internalencoding']='UTF-8'; 必须要用 ISO-8859-1,否则就会报错。

    [code]
    <?php
    require("./xmlrpc/lib/xmlrpc.inc");
    // 这里就是解决商品名称中有中文时的乱码问题
    //$GLOBALS['xmlrpc_internalencoding']='UTF-8'; // 如果从里定义的话,那么 create、write 方法就会出现 500 错误。
    class XmlrpcOe{
        public $user = 'admin';
        public $password = 'admin';
        public $user_id = -1;
        public $dbname = 'test';
        public $server_url = 'http://127.0.0.1:8069/xmlrpc/';
        public function __construct() {
            $this->sock_common();
            $this->sock_object();
            $this->connect();
        }

        public function sock_common() {
            $this->sock_com = new xmlrpc_client($this->server_url.'common');
        }
        public function sock_object() {
            $this->sock_obj = new xmlrpc_client($this->server_url.'object');
        }
        /
        * 登录,获取 user_id
        /
        public function connect() {
            /

            if(isset($_COOKIE["user_id"]) == true)  {
                if($_COOKIE["user_id"]>0) {
                    return $_COOKIE["user_id"];
                }
            }
            /
            $msg = new xmlrpcmsg('login');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user, "string"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $resp =  $this->sock_com->send($msg);
            $val = $resp->value();
            $this->user_id = $val->scalarval();
            setcookie("user_id",$this->user_id,time()+3600);
        }

        /

        * 搜索-单条件
        * $relation: 搜索的对象,如:product.category
        * $attribute: 属性,如:name
        * $operator: 条件,如:>、=、!=、like
        * $keys: 值,如:p
        /
        public function search($relation, $attribute, $operator, $keys) {
            $GLOBALS['xmlrpc_internalencoding']='UTF-8';
            $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
                new xmlrpcval($operator,"string"),
                new xmlrpcval($keys,"string")),"array"),);
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("search", "string"));
            $msg->addParam(new xmlrpcval($key, "array"));
            $resp = $this->sock_obj->send($msg);
            $val = $resp->value();
            $ids_rpc_array = $val->scalarval();
            $ids = array();
            foreach($ids_rpc_array as $id_obj) {
                $ids[] = $id_obj->me["int"];
            }
            return $ids_rpc_array;
            //return $ids;
        }
        /

        * 读数据,通过 id 读取相关数据
        * $relation: 数据对象,如:product.product
        * $ids: id 的数组,如:array(new xmlrpcval(4, "int"));
        * $field_array: 字段数组
        */
        public function read($relation, $ids, $field_array) {
            $GLOBALS['xmlrpc_internalencoding']='UTF-8';
           
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("read", "string"));
            $msg->addParam(new xmlrpcval($ids, "array"));
            $msg->addParam(new xmlrpcval($field_array, "array"));
            $resp = $this->sock_obj->send($msg);
            $val = $resp->value();
            return $val->scalarval();
        }
        public function create($relation, $partner_data) {
            $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("create", "string"));
            $msg->addParam(new xmlrpcval($partner_data, "struct"));
            $resp = $this->sock_obj->send($msg);
            if ($resp->faultCode())
                echo 'Error: '.$resp->faultString();
            else
                echo 'Partner '.$resp->value()->scalarval().' created !';
        }
        public function write($relation, $data, $ids) {
            $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("write", "string"));
            $msg->addParam(new xmlrpcval($ids, "array"));
            $msg->addParam(new xmlrpcval($data, "struct"));
            $resp = $this->sock_obj->send($msg);
            $val = $resp->value();
            return $val->scalarval();
        }
    }
    $xmlrpc_oe = new XmlrpcOe();
    # 搜索
    echo "search:<br />";
    $ids = $xmlrpc_oe->search("product.product", "id", ">", "1");
    print_r($ids);
    echo "<br /><br />";

    $id_array = array();
    foreach($ids as $id_obj) {
        $id_array[] = $id_obj->me["int"];
    }
    print_r($id_array);
    echo "<br /><br />---------------------------------------------------------<br />";

    # 读数据
    echo "read:<br />";
    $field_array = array(
            new xmlrpcval("id", "int"),
            new xmlrpcval("name", "string"),
            new xmlrpcval("list_price", "string"),
        );
    $ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);
    print_r($ret_array);
    echo "<br /><br />";
    $out_array = array();
    foreach($ret_array as $k => $v) {
        $tmp_id = $v->me['struct']['id']->me["int"];
        $tmp_name = $v->me['struct']['name']->me["string"];
        $tmp_price = $v->me['struct']['list_price']->me["double"];
        $out_array[] = array(
                "id" => $tmp_id,
                "name" => $tmp_name,
                "list_price" => $tmp_price,
            );
    }
    print_r($out_array);
    echo "<br /><br />---------------------------------------------------------<br />";

    # 添加
    echo "create:<br />";
    $partner_data = array(
            "name" => new xmlrpcval("魅族 MX2 32GB", "string"),
            "list_price" => new xmlrpcval(2999.00, "double"),
        );
    //$xmlrpc_oe->create("product.product", $partner_data);
    echo "<br /><br />---------------------------------------------------------<br />";

    # 修改
    echo "write:<br />";
    $ids = array(
            new xmlrpcval(7, "int"),
        );
    $data = array(
            "name" => new xmlrpcval("魅族 MX2 64GB", "string"),
            "list_price" => new xmlrpcval(3999.00, "double"),
        );
    $xmlrpc_oe->write("product.product", $data, $ids);
    $ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);
    $out_array = array();
    foreach($ret_array as $k => $v) {
        $tmp_id = $v->me['struct']['id']->me["int"];
        $tmp_name = $v->me['struct']['name']->me["string"];
        $tmp_price = $v->me['struct']['list_price']->me["double"];
        $out_array[] = array(
                "id" => $tmp_id,
                "name" => $tmp_name,
                "list_price" => $tmp_price,
            );
    }
    print_r($out_array);

    [/code]

    参考:
    [url=http://shine-it.net/index.php/topic,848.msg3892.html]php xmlrpc 接口调用实例(商品数据获取)[/url]
    [url=http://doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/index.html]XML-RPC Web services[/url]



  • php 操作 OE 的方法

    phpxmlrpc 版本:v2.2.2([url=http://phpxmlrpc.sourceforge.net/]下载[/url])
    openerp 版本:v7.0-20130103-002220-1
    LNMP 版本:v0.9

    * 搜索
    * 读取
    * 创建
    * 更新。

    如果报 "Call to a member function scalarval() on a non-object" 错,请看看是不是 fsockopen 函数被禁用。
    * vi /usr/local/php/etc/php.ini
    * 搜索 disable_functions, 删除里面的 fsockopen

    还有个问题,创建和更新的方法,不能用 $GLOBALS['xmlrpc_internalencoding']='UTF-8'; 必须要用 ISO-8859-1,否则就会报错。

    [code]
    <?php
    require("./xmlrpc/lib/xmlrpc.inc");
    // 这里就是解决商品名称中有中文时的乱码问题
    //$GLOBALS['xmlrpc_internalencoding']='UTF-8'; // 如果从里定义的话,那么 create、write 方法就会出现 500 错误。
    class XmlrpcOe{
        public $user = 'admin';
        public $password = 'admin';
        public $user_id = -1;
        public $dbname = 'test';
        public $server_url = 'http://127.0.0.1:8069/xmlrpc/';
        public function __construct() {
            $this->sock_common();
            $this->sock_object();
            $this->connect();
        }

        public function sock_common() {
            $this->sock_com = new xmlrpc_client($this->server_url.'common');
        }
        public function sock_object() {
            $this->sock_obj = new xmlrpc_client($this->server_url.'object');
        }
        /
        * 登录,获取 user_id
        /
        public function connect() {
            /

            if(isset($_COOKIE["user_id"]) == true)  {
                if($_COOKIE["user_id"]>0) {
                    return $_COOKIE["user_id"];
                }
            }
            /
            $msg = new xmlrpcmsg('login');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user, "string"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $resp =  $this->sock_com->send($msg);
            $val = $resp->value();
            $this->user_id = $val->scalarval();
            setcookie("user_id",$this->user_id,time()+3600);
        }

        /

        * 搜索-单条件
        * $relation: 搜索的对象,如:product.category
        * $attribute: 属性,如:name
        * $operator: 条件,如:>、=、!=、like
        * $keys: 值,如:p
        /
        public function search($relation, $attribute, $operator, $keys) {
            $GLOBALS['xmlrpc_internalencoding']='UTF-8';
            $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),
                new xmlrpcval($operator,"string"),
                new xmlrpcval($keys,"string")),"array"),);
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("search", "string"));
            $msg->addParam(new xmlrpcval($key, "array"));
            $resp = $this->sock_obj->send($msg);
            $val = $resp->value();
            $ids_rpc_array = $val->scalarval();
            $ids = array();
            foreach($ids_rpc_array as $id_obj) {
                $ids[] = $id_obj->me["int"];
            }
            return $ids_rpc_array;
            //return $ids;
        }
        /

        * 读数据,通过 id 读取相关数据
        * $relation: 数据对象,如:product.product
        * $ids: id 的数组,如:array(new xmlrpcval(4, "int"));
        * $field_array: 字段数组
        */
        public function read($relation, $ids, $field_array) {
            $GLOBALS['xmlrpc_internalencoding']='UTF-8';
           
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("read", "string"));
            $msg->addParam(new xmlrpcval($ids, "array"));
            $msg->addParam(new xmlrpcval($field_array, "array"));
            $resp = $this->sock_obj->send($msg);
            $val = $resp->value();
            return $val->scalarval();
        }
        public function create($relation, $partner_data) {
            $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("create", "string"));
            $msg->addParam(new xmlrpcval($partner_data, "struct"));
            $resp = $this->sock_obj->send($msg);
            if ($resp->faultCode())
                echo 'Error: '.$resp->faultString();
            else
                echo 'Partner '.$resp->value()->scalarval().' created !';
        }
        public function write($relation, $data, $ids) {
            $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';
            $msg = new xmlrpcmsg('execute');
            $msg->addParam(new xmlrpcval($this->dbname, "string"));
            $msg->addParam(new xmlrpcval($this->user_id, "int"));
            $msg->addParam(new xmlrpcval($this->password, "string"));
            $msg->addParam(new xmlrpcval($relation, "string"));
            $msg->addParam(new xmlrpcval("write", "string"));
            $msg->addParam(new xmlrpcval($ids, "array"));
            $msg->addParam(new xmlrpcval($data, "struct"));
            $resp = $this->sock_obj->send($msg);
            $val = $resp->value();
            return $val->scalarval();
        }
    }
    $xmlrpc_oe = new XmlrpcOe();
    # 搜索
    echo "search:<br />";
    $ids = $xmlrpc_oe->search("product.product", "id", ">", "1");
    print_r($ids);
    echo "<br /><br />";

    $id_array = array();
    foreach($ids as $id_obj) {
        $id_array[] = $id_obj->me["int"];
    }
    print_r($id_array);
    echo "<br /><br />---------------------------------------------------------<br />";

    # 读数据
    echo "read:<br />";
    $field_array = array(
            new xmlrpcval("id", "int"),
            new xmlrpcval("name", "string"),
            new xmlrpcval("list_price", "string"),
        );
    $ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);
    print_r($ret_array);
    echo "<br /><br />";
    $out_array = array();
    foreach($ret_array as $k => $v) {
        $tmp_id = $v->me['struct']['id']->me["int"];
        $tmp_name = $v->me['struct']['name']->me["string"];
        $tmp_price = $v->me['struct']['list_price']->me["double"];
        $out_array[] = array(
                "id" => $tmp_id,
                "name" => $tmp_name,
                "list_price" => $tmp_price,
            );
    }
    print_r($out_array);
    echo "<br /><br />---------------------------------------------------------<br />";

    # 添加
    echo "create:<br />";
    $partner_data = array(
            "name" => new xmlrpcval("魅族 MX2 32GB", "string"),
            "list_price" => new xmlrpcval(2999.00, "double"),
        );
    //$xmlrpc_oe->create("product.product", $partner_data);
    echo "<br /><br />---------------------------------------------------------<br />";

    # 修改
    echo "write:<br />";
    $ids = array(
            new xmlrpcval(7, "int"),
        );
    $data = array(
            "name" => new xmlrpcval("魅族 MX2 64GB", "string"),
            "list_price" => new xmlrpcval(3999.00, "double"),
        );
    $xmlrpc_oe->write("product.product", $data, $ids);
    $ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);
    $out_array = array();
    foreach($ret_array as $k => $v) {
        $tmp_id = $v->me['struct']['id']->me["int"];
        $tmp_name = $v->me['struct']['name']->me["string"];
        $tmp_price = $v->me['struct']['list_price']->me["double"];
        $out_array[] = array(
                "id" => $tmp_id,
                "name" => $tmp_name,
                "list_price" => $tmp_price,
            );
    }
    print_r($out_array);

    [/code]

    参考:
    [url=http://shine-it.net/index.php/topic,848.msg3892.html]php xmlrpc 接口调用实例(商品数据获取)[/url]
    [url=http://doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/index.html]XML-RPC Web services[/url]



  • 不错.....  赞一个先...



  • 如果我想执行sql语句、该怎么写呢? 又或者关联表查询? 求助!!!



  • 为什么还要直接执行 SQL 语句。。知道 ORM 的概念吗?了解一些 ORM 的操作模式(就是面向对象),你的问题也就不是问题了


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待