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

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

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

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

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



  • php 操作 OE 的方法<br /><br />phpxmlrpc 版本:v2.2.2([url=http://phpxmlrpc.sourceforge.net/]下载[/url])<br />openerp 版本:v7.0-20130103-002220-1<br />LNMP 版本:v0.9<br /><br />* 搜索<br />* 读取<br />* 创建<br />* 更新。<br /><br />如果报 "Call to a member function scalarval() on a non-object" 错,请看看是不是 fsockopen 函数被禁用。<br />* vi /usr/local/php/etc/php.ini <br />* 搜索 disable_functions, 删除里面的 fsockopen<br /><br />还有个问题,创建和更新的方法,不能用 $GLOBALS['xmlrpc_internalencoding']='UTF-8'; 必须要用 ISO-8859-1,否则就会报错。<br /> <br />[code] <br /><?php<br />require("./xmlrpc/lib/xmlrpc.inc");<br />// 这里就是解决商品名称中有中文时的乱码问题<br />//$GLOBALS['xmlrpc_internalencoding']='UTF-8'; // 如果从里定义的话,那么 create、write 方法就会出现 500 错误。<br />class XmlrpcOe{<br />    public $user = 'admin';<br />    public $password = 'admin';<br />    public $user_id = -1;<br />    public $dbname = 'test';<br />    public $server_url = 'http://127.0.0.1:8069/xmlrpc/';<br />    public function __construct() {<br />        $this->sock_common();<br />        $this->sock_object();<br />        $this->connect();<br />    }<br /><br />    public function sock_common() {<br />        $this->sock_com = new xmlrpc_client($this->server_url.'common');<br />    }<br />    public function sock_object() {<br />        $this->sock_obj = new xmlrpc_client($this->server_url.'object');<br />    }<br />    /<br />    * 登录,获取 user_id<br />    /<br />    public function connect() {<br />        /<br />        if(isset($_COOKIE["user_id"]) == true)  {<br />            if($_COOKIE["user_id"]>0) {<br />                return $_COOKIE["user_id"];<br />            }<br />        }<br />        /<br />        $msg = new xmlrpcmsg('login');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user, "string"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $resp =  $this->sock_com->send($msg);<br />        $val = $resp->value();<br />        $this->user_id = $val->scalarval();<br />        setcookie("user_id",$this->user_id,time()+3600);<br />    }<br /><br />    /<br />    * 搜索-单条件<br />    * $relation: 搜索的对象,如:product.category<br />    * $attribute: 属性,如:name<br />    * $operator: 条件,如:>、=、!=、like<br />    * $keys: 值,如:p<br />    /<br />    public function search($relation, $attribute, $operator, $keys) {<br />        $GLOBALS['xmlrpc_internalencoding']='UTF-8';<br />        $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),<br />            new xmlrpcval($operator,"string"),<br />            new xmlrpcval($keys,"string")),"array"),);<br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("search", "string"));<br />        $msg->addParam(new xmlrpcval($key, "array"));<br />        $resp = $this->sock_obj->send($msg);<br />        $val = $resp->value();<br />        $ids_rpc_array = $val->scalarval();<br />        $ids = array();<br />        foreach($ids_rpc_array as $id_obj) {<br />            $ids[] = $id_obj->me["int"];<br />        }<br />        return $ids_rpc_array;<br />        //return $ids;<br />    }<br />    /<br />    * 读数据,通过 id 读取相关数据<br />    * $relation: 数据对象,如:product.product<br />    * $ids: id 的数组,如:array(new xmlrpcval(4, "int"));<br />    * $field_array: 字段数组<br />    */<br />    public function read($relation, $ids, $field_array) {<br />        $GLOBALS['xmlrpc_internalencoding']='UTF-8';<br />        <br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("read", "string"));<br />        $msg->addParam(new xmlrpcval($ids, "array"));<br />        $msg->addParam(new xmlrpcval($field_array, "array"));<br />        $resp = $this->sock_obj->send($msg);<br />        $val = $resp->value();<br />        return $val->scalarval();<br />    }<br />    public function create($relation, $partner_data) {<br />        $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';<br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("create", "string"));<br />        $msg->addParam(new xmlrpcval($partner_data, "struct"));<br />        $resp = $this->sock_obj->send($msg);<br />        if ($resp->faultCode())<br />            echo 'Error: '.$resp->faultString();<br />        else<br />            echo 'Partner '.$resp->value()->scalarval().' created !';<br />    }<br />    public function write($relation, $data, $ids) {<br />        $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';<br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("write", "string"));<br />        $msg->addParam(new xmlrpcval($ids, "array"));<br />        $msg->addParam(new xmlrpcval($data, "struct"));<br />        $resp = $this->sock_obj->send($msg);<br />        $val = $resp->value();<br />        return $val->scalarval();<br />    }<br />}<br />$xmlrpc_oe = new XmlrpcOe();<br /># 搜索<br />echo "search:<br />";<br />$ids = $xmlrpc_oe->search("product.product", "id", ">", "1");<br />print_r($ids);<br />echo "<br /><br />";<br /><br />$id_array = array();<br />foreach($ids as $id_obj) {<br />    $id_array[] = $id_obj->me["int"];<br />}<br />print_r($id_array);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /> <br /># 读数据<br />echo "read:<br />";<br />$field_array = array(<br />        new xmlrpcval("id", "int"),<br />        new xmlrpcval("name", "string"),<br />        new xmlrpcval("list_price", "string"),<br />    );<br />$ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);<br />print_r($ret_array);<br />echo "<br /><br />";<br />$out_array = array();<br />foreach($ret_array as $k => $v) {<br />    $tmp_id = $v->me['struct']['id']->me["int"];<br />    $tmp_name = $v->me['struct']['name']->me["string"];<br />    $tmp_price = $v->me['struct']['list_price']->me["double"];<br />    $out_array[] = array(<br />            "id" => $tmp_id,<br />            "name" => $tmp_name,<br />            "list_price" => $tmp_price,<br />        );<br />}<br />print_r($out_array);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /><br /># 添加<br />echo "create:<br />";<br />$partner_data = array(<br />        "name" => new xmlrpcval("魅族 MX2 32GB", "string"),<br />        "list_price" => new xmlrpcval(2999.00, "double"),<br />    );<br />//$xmlrpc_oe->create("product.product", $partner_data);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /><br /># 修改<br />echo "write:<br />";<br />$ids = array(<br />        new xmlrpcval(7, "int"),<br />    );<br />$data = array(<br />        "name" => new xmlrpcval("魅族 MX2 64GB", "string"),<br />        "list_price" => new xmlrpcval(3999.00, "double"),<br />    );<br />$xmlrpc_oe->write("product.product", $data, $ids);<br />$ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);<br />$out_array = array();<br />foreach($ret_array as $k => $v) {<br />    $tmp_id = $v->me['struct']['id']->me["int"];<br />    $tmp_name = $v->me['struct']['name']->me["string"];<br />    $tmp_price = $v->me['struct']['list_price']->me["double"];<br />    $out_array[] = array(<br />            "id" => $tmp_id,<br />            "name" => $tmp_name,<br />            "list_price" => $tmp_price,<br />        );<br />}<br />print_r($out_array);<br /><br />[/code]<br /> <br />参考:<br />[url=http://shine-it.net/index.php/topic,848.msg3892.html]php xmlrpc 接口调用实例(商品数据获取)[/url]<br />[url=http://doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/index.html]XML-RPC Web services[/url]



  • php 操作 OE 的方法<br /><br />phpxmlrpc 版本:v2.2.2([url=http://phpxmlrpc.sourceforge.net/]下载[/url])<br />openerp 版本:v7.0-20130103-002220-1<br />LNMP 版本:v0.9<br /><br />* 搜索<br />* 读取<br />* 创建<br />* 更新。<br /><br />如果报 "Call to a member function scalarval() on a non-object" 错,请看看是不是 fsockopen 函数被禁用。<br />* vi /usr/local/php/etc/php.ini <br />* 搜索 disable_functions, 删除里面的 fsockopen<br /><br />还有个问题,创建和更新的方法,不能用 $GLOBALS['xmlrpc_internalencoding']='UTF-8'; 必须要用 ISO-8859-1,否则就会报错。<br /> <br />[code] <br /><?php<br />require("./xmlrpc/lib/xmlrpc.inc");<br />// 这里就是解决商品名称中有中文时的乱码问题<br />//$GLOBALS['xmlrpc_internalencoding']='UTF-8'; // 如果从里定义的话,那么 create、write 方法就会出现 500 错误。<br />class XmlrpcOe{<br />    public $user = 'admin';<br />    public $password = 'admin';<br />    public $user_id = -1;<br />    public $dbname = 'test';<br />    public $server_url = 'http://127.0.0.1:8069/xmlrpc/';<br />    public function __construct() {<br />        $this->sock_common();<br />        $this->sock_object();<br />        $this->connect();<br />    }<br /><br />    public function sock_common() {<br />        $this->sock_com = new xmlrpc_client($this->server_url.'common');<br />    }<br />    public function sock_object() {<br />        $this->sock_obj = new xmlrpc_client($this->server_url.'object');<br />    }<br />    /<br />    * 登录,获取 user_id<br />    /<br />    public function connect() {<br />        /<br />        if(isset($_COOKIE["user_id"]) == true)  {<br />            if($_COOKIE["user_id"]>0) {<br />                return $_COOKIE["user_id"];<br />            }<br />        }<br />        /<br />        $msg = new xmlrpcmsg('login');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user, "string"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $resp =  $this->sock_com->send($msg);<br />        $val = $resp->value();<br />        $this->user_id = $val->scalarval();<br />        setcookie("user_id",$this->user_id,time()+3600);<br />    }<br /><br />    /<br />    * 搜索-单条件<br />    * $relation: 搜索的对象,如:product.category<br />    * $attribute: 属性,如:name<br />    * $operator: 条件,如:>、=、!=、like<br />    * $keys: 值,如:p<br />    /<br />    public function search($relation, $attribute, $operator, $keys) {<br />        $GLOBALS['xmlrpc_internalencoding']='UTF-8';<br />        $key = array(new xmlrpcval(array(new xmlrpcval($attribute , "string"),<br />            new xmlrpcval($operator,"string"),<br />            new xmlrpcval($keys,"string")),"array"),);<br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("search", "string"));<br />        $msg->addParam(new xmlrpcval($key, "array"));<br />        $resp = $this->sock_obj->send($msg);<br />        $val = $resp->value();<br />        $ids_rpc_array = $val->scalarval();<br />        $ids = array();<br />        foreach($ids_rpc_array as $id_obj) {<br />            $ids[] = $id_obj->me["int"];<br />        }<br />        return $ids_rpc_array;<br />        //return $ids;<br />    }<br />    /<br />    * 读数据,通过 id 读取相关数据<br />    * $relation: 数据对象,如:product.product<br />    * $ids: id 的数组,如:array(new xmlrpcval(4, "int"));<br />    * $field_array: 字段数组<br />    */<br />    public function read($relation, $ids, $field_array) {<br />        $GLOBALS['xmlrpc_internalencoding']='UTF-8';<br />        <br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("read", "string"));<br />        $msg->addParam(new xmlrpcval($ids, "array"));<br />        $msg->addParam(new xmlrpcval($field_array, "array"));<br />        $resp = $this->sock_obj->send($msg);<br />        $val = $resp->value();<br />        return $val->scalarval();<br />    }<br />    public function create($relation, $partner_data) {<br />        $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';<br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("create", "string"));<br />        $msg->addParam(new xmlrpcval($partner_data, "struct"));<br />        $resp = $this->sock_obj->send($msg);<br />        if ($resp->faultCode())<br />            echo 'Error: '.$resp->faultString();<br />        else<br />            echo 'Partner '.$resp->value()->scalarval().' created !';<br />    }<br />    public function write($relation, $data, $ids) {<br />        $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';<br />        $msg = new xmlrpcmsg('execute');<br />        $msg->addParam(new xmlrpcval($this->dbname, "string"));<br />        $msg->addParam(new xmlrpcval($this->user_id, "int"));<br />        $msg->addParam(new xmlrpcval($this->password, "string"));<br />        $msg->addParam(new xmlrpcval($relation, "string"));<br />        $msg->addParam(new xmlrpcval("write", "string"));<br />        $msg->addParam(new xmlrpcval($ids, "array"));<br />        $msg->addParam(new xmlrpcval($data, "struct"));<br />        $resp = $this->sock_obj->send($msg);<br />        $val = $resp->value();<br />        return $val->scalarval();<br />    }<br />}<br />$xmlrpc_oe = new XmlrpcOe();<br /># 搜索<br />echo "search:<br />";<br />$ids = $xmlrpc_oe->search("product.product", "id", ">", "1");<br />print_r($ids);<br />echo "<br /><br />";<br /><br />$id_array = array();<br />foreach($ids as $id_obj) {<br />    $id_array[] = $id_obj->me["int"];<br />}<br />print_r($id_array);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /> <br /># 读数据<br />echo "read:<br />";<br />$field_array = array(<br />        new xmlrpcval("id", "int"),<br />        new xmlrpcval("name", "string"),<br />        new xmlrpcval("list_price", "string"),<br />    );<br />$ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);<br />print_r($ret_array);<br />echo "<br /><br />";<br />$out_array = array();<br />foreach($ret_array as $k => $v) {<br />    $tmp_id = $v->me['struct']['id']->me["int"];<br />    $tmp_name = $v->me['struct']['name']->me["string"];<br />    $tmp_price = $v->me['struct']['list_price']->me["double"];<br />    $out_array[] = array(<br />            "id" => $tmp_id,<br />            "name" => $tmp_name,<br />            "list_price" => $tmp_price,<br />        );<br />}<br />print_r($out_array);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /><br /># 添加<br />echo "create:<br />";<br />$partner_data = array(<br />        "name" => new xmlrpcval("魅族 MX2 32GB", "string"),<br />        "list_price" => new xmlrpcval(2999.00, "double"),<br />    );<br />//$xmlrpc_oe->create("product.product", $partner_data);<br />echo "<br /><br />---------------------------------------------------------<br />";<br /><br /># 修改<br />echo "write:<br />";<br />$ids = array(<br />        new xmlrpcval(7, "int"),<br />    );<br />$data = array(<br />        "name" => new xmlrpcval("魅族 MX2 64GB", "string"),<br />        "list_price" => new xmlrpcval(3999.00, "double"),<br />    );<br />$xmlrpc_oe->write("product.product", $data, $ids);<br />$ret_array = $xmlrpc_oe->read("product.product", $ids, $field_array);<br />$out_array = array();<br />foreach($ret_array as $k => $v) {<br />    $tmp_id = $v->me['struct']['id']->me["int"];<br />    $tmp_name = $v->me['struct']['name']->me["string"];<br />    $tmp_price = $v->me['struct']['list_price']->me["double"];<br />    $out_array[] = array(<br />            "id" => $tmp_id,<br />            "name" => $tmp_name,<br />            "list_price" => $tmp_price,<br />        );<br />}<br />print_r($out_array);<br /><br />[/code]<br /> <br />参考:<br />[url=http://shine-it.net/index.php/topic,848.msg3892.html]php xmlrpc 接口调用实例(商品数据获取)[/url]<br />[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 中文社区 的连接断开,我们正在尝试重连,请耐心等待