跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. Xmlrpc,php 操作 OE 的方法 --- 搜索、读取、创建、更新

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

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
5 帖子 3 发布者 4.7k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • F 离线
    F 离线
    fxiao
    写于 最后由 编辑
    #1

    php 操作 OE 的方法

    phpxmlrpc 版本:v2.2.2( http://phpxmlrpc.sourceforge.net/ )
    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,否则就会报错。

     <br />&lt;?php<br />require(&quot;./xmlrpc/lib/xmlrpc.inc&quot;);<br />// 这里就是解决商品名称中有中文时的乱码问题<br />//$GLOBALS&#91;&#039;xmlrpc_internalencoding&#039;]=&#039;UTF-8&#039;; // 如果从里定义的话,那么 create、write 方法就会出现 500 错误。<br />class XmlrpcOe{<br />&nbsp; &nbsp; public $user = &#039;admin&#039;;<br />&nbsp; &nbsp; public $password = &#039;admin&#039;;<br />&nbsp; &nbsp; public $user_id = -1;<br />&nbsp; &nbsp; public $dbname = &#039;test&#039;;<br />&nbsp; &nbsp; public $server_url = &#039;http://127.0.0.1:8069/xmlrpc/&#039;;<br />&nbsp; &nbsp; public function __construct() {<br />&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;sock_common();<br />&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;sock_object();<br />&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;connect();<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; public function sock_common() {<br />&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;sock_com = new xmlrpc_client($this-&gt;server_url.&#039;common&#039;);<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; public function sock_object() {<br />&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;sock_obj = new xmlrpc_client($this-&gt;server_url.&#039;object&#039;);<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; /**<br />&nbsp; &nbsp;  * 登录,获取 user_id<br />&nbsp; &nbsp;  */<br />&nbsp; &nbsp; public function connect() {<br />&nbsp; &nbsp; &nbsp; &nbsp; /*<br />&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_COOKIE[&quot;user_id&quot;]) == true)&nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($_COOKIE[&quot;user_id&quot;]&gt;0) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $_COOKIE[&quot;user_id&quot;];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp;  */<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg = new xmlrpcmsg(&#039;login&#039;);<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;dbname, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;user, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;password, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $resp =&nbsp; $this-&gt;sock_com-&gt;send($msg);<br />&nbsp; &nbsp; &nbsp; &nbsp; $val = $resp-&gt;value();<br />&nbsp; &nbsp; &nbsp; &nbsp; $this-&gt;user_id = $val-&gt;scalarval();<br />&nbsp; &nbsp; &nbsp; &nbsp; setcookie(&quot;user_id&quot;,$this-&gt;user_id,time()+3600);<br />&nbsp; &nbsp; }<br /><br />&nbsp; &nbsp; /**<br />&nbsp; &nbsp;  * 搜索-单条件<br />&nbsp; &nbsp;  * $relation: 搜索的对象,如:product.category<br />&nbsp; &nbsp;  * $attribute: 属性,如:name<br />&nbsp; &nbsp;  * $operator: 条件,如:&gt;、=、!=、like<br />&nbsp; &nbsp;  * $keys: 值,如:p<br />&nbsp; &nbsp;  */<br />&nbsp; &nbsp; public function search($relation, $attribute, $operator, $keys) {<br />&nbsp; &nbsp; &nbsp; &nbsp; $GLOBALS&#91;&#039;xmlrpc_internalencoding&#039;]=&#039;UTF-8&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; $key = array(new xmlrpcval(array(new xmlrpcval($attribute , &quot;string&quot;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new xmlrpcval($operator,&quot;string&quot;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new xmlrpcval($keys,&quot;string&quot;)),&quot;array&quot;),);<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg = new xmlrpcmsg(&#039;execute&#039;);<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;dbname, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;user_id, &quot;int&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;password, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($relation, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval(&quot;search&quot;, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($key, &quot;array&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $resp = $this-&gt;sock_obj-&gt;send($msg);<br />&nbsp; &nbsp; &nbsp; &nbsp; $val = $resp-&gt;value();<br />&nbsp; &nbsp; &nbsp; &nbsp; $ids_rpc_array = $val-&gt;scalarval();<br />&nbsp; &nbsp; &nbsp; &nbsp; $ids = array();<br />&nbsp; &nbsp; &nbsp; &nbsp; foreach($ids_rpc_array as $id_obj) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ids&#91;] = $id_obj-&gt;me[&quot;int&quot;];<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; return $ids_rpc_array;<br />&nbsp; &nbsp; &nbsp; &nbsp; //return $ids;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; /**<br />&nbsp; &nbsp;  * 读数据,通过 id 读取相关数据<br />&nbsp; &nbsp;  * $relation: 数据对象,如:product.product<br />&nbsp; &nbsp;  * $ids: id 的数组,如:array(new xmlrpcval(4, &quot;int&quot;));<br />&nbsp; &nbsp;  * $field_array: 字段数组<br />&nbsp; &nbsp;  */<br />&nbsp; &nbsp; public function read($relation, $ids, $field_array) {<br />&nbsp; &nbsp; &nbsp; &nbsp; $GLOBALS&#91;&#039;xmlrpc_internalencoding&#039;]=&#039;UTF-8&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; $msg = new xmlrpcmsg(&#039;execute&#039;);<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;dbname, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;user_id, &quot;int&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;password, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($relation, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval(&quot;read&quot;, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($ids, &quot;array&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($field_array, &quot;array&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $resp = $this-&gt;sock_obj-&gt;send($msg);<br />&nbsp; &nbsp; &nbsp; &nbsp; $val = $resp-&gt;value();<br />&nbsp; &nbsp; &nbsp; &nbsp; return $val-&gt;scalarval();<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; public function create($relation, $partner_data) {<br />&nbsp; &nbsp; &nbsp; &nbsp; $GLOBALS&#91;&#039;xmlrpc_internalencoding&#039;]=&#039;ISO-8859-1&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg = new xmlrpcmsg(&#039;execute&#039;);<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;dbname, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;user_id, &quot;int&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;password, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($relation, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval(&quot;create&quot;, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($partner_data, &quot;struct&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $resp = $this-&gt;sock_obj-&gt;send($msg);<br />&nbsp; &nbsp; &nbsp; &nbsp; if ($resp-&gt;faultCode())<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#039;Error: &#039;.$resp-&gt;faultString();<br />&nbsp; &nbsp; &nbsp; &nbsp; else<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo &#039;Partner &#039;.$resp-&gt;value()-&gt;scalarval().&#039; created !&#039;;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; public function write($relation, $data, $ids) {<br />&nbsp; &nbsp; &nbsp; &nbsp; $GLOBALS&#91;&#039;xmlrpc_internalencoding&#039;]=&#039;ISO-8859-1&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg = new xmlrpcmsg(&#039;execute&#039;);<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;dbname, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;user_id, &quot;int&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($this-&gt;password, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($relation, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval(&quot;write&quot;, &quot;string&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($ids, &quot;array&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $msg-&gt;addParam(new xmlrpcval($data, &quot;struct&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; $resp = $this-&gt;sock_obj-&gt;send($msg);<br />&nbsp; &nbsp; &nbsp; &nbsp; $val = $resp-&gt;value();<br />&nbsp; &nbsp; &nbsp; &nbsp; return $val-&gt;scalarval();<br />&nbsp; &nbsp; }<br />}<br />$xmlrpc_oe = new XmlrpcOe();<br /># 搜索<br />echo &quot;search:&lt;br /&gt;&quot;;<br />$ids = $xmlrpc_oe-&gt;search(&quot;product.product&quot;, &quot;id&quot;, &quot;&gt;&quot;, &quot;1&quot;);<br />print_r($ids);<br />echo &quot;&lt;br /&gt;&lt;br /&gt;&quot;;<br /><br />$id_array = array();<br />foreach($ids as $id_obj) {<br />&nbsp; &nbsp; $id_array&#91;] = $id_obj-&gt;me[&quot;int&quot;];<br />}<br />print_r($id_array);<br />echo &quot;&lt;br /&gt;&lt;br /&gt;---------------------------------------------------------&lt;br /&gt;&quot;;<br /> <br /># 读数据<br />echo &quot;read:&lt;br /&gt;&quot;;<br />$field_array = array(<br />&nbsp; &nbsp; &nbsp; &nbsp; new xmlrpcval(&quot;id&quot;, &quot;int&quot;),<br />&nbsp; &nbsp; &nbsp; &nbsp; new xmlrpcval(&quot;name&quot;, &quot;string&quot;),<br />&nbsp; &nbsp; &nbsp; &nbsp; new xmlrpcval(&quot;list_price&quot;, &quot;string&quot;),<br />&nbsp; &nbsp; );<br />$ret_array = $xmlrpc_oe-&gt;read(&quot;product.product&quot;, $ids, $field_array);<br />print_r($ret_array);<br />echo &quot;&lt;br /&gt;&lt;br /&gt;&quot;;<br />$out_array = array();<br />foreach($ret_array as $k =&gt; $v) {<br />&nbsp; &nbsp; $tmp_id = $v-&gt;me&#91;&#039;struct&#039;]&#91;&#039;id&#039;]-&gt;me[&quot;int&quot;];<br />&nbsp; &nbsp; $tmp_name = $v-&gt;me&#91;&#039;struct&#039;]&#91;&#039;name&#039;]-&gt;me[&quot;string&quot;];<br />&nbsp; &nbsp; $tmp_price = $v-&gt;me&#91;&#039;struct&#039;]&#91;&#039;list_price&#039;]-&gt;me[&quot;double&quot;];<br />&nbsp; &nbsp; $out_array&#91;] = array(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;id&quot; =&gt; $tmp_id,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot; =&gt; $tmp_name,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;list_price&quot; =&gt; $tmp_price,<br />&nbsp; &nbsp; &nbsp; &nbsp; );<br />}<br />print_r($out_array);<br />echo &quot;&lt;br /&gt;&lt;br /&gt;---------------------------------------------------------&lt;br /&gt;&quot;;<br /><br /># 添加<br />echo &quot;create:&lt;br /&gt;&quot;;<br />$partner_data = array(<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot; =&gt; new xmlrpcval(&quot;魅族 MX2 32GB&quot;, &quot;string&quot;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;list_price&quot; =&gt; new xmlrpcval(2999.00, &quot;double&quot;),<br />&nbsp; &nbsp; );<br />//$xmlrpc_oe-&gt;create(&quot;product.product&quot;, $partner_data);<br />echo &quot;&lt;br /&gt;&lt;br /&gt;---------------------------------------------------------&lt;br /&gt;&quot;;<br /><br /># 修改<br />echo &quot;write:&lt;br /&gt;&quot;;<br />$ids = array(<br />&nbsp; &nbsp; &nbsp; &nbsp; new xmlrpcval(7, &quot;int&quot;),<br />&nbsp; &nbsp; );<br />$data = array(<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot; =&gt; new xmlrpcval(&quot;魅族 MX2 64GB&quot;, &quot;string&quot;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;list_price&quot; =&gt; new xmlrpcval(3999.00, &quot;double&quot;),<br />&nbsp; &nbsp; );<br />$xmlrpc_oe-&gt;write(&quot;product.product&quot;, $data, $ids);<br />$ret_array = $xmlrpc_oe-&gt;read(&quot;product.product&quot;, $ids, $field_array);<br />$out_array = array();<br />foreach($ret_array as $k =&gt; $v) {<br />&nbsp; &nbsp; $tmp_id = $v-&gt;me&#91;&#039;struct&#039;]&#91;&#039;id&#039;]-&gt;me[&quot;int&quot;];<br />&nbsp; &nbsp; $tmp_name = $v-&gt;me&#91;&#039;struct&#039;]&#91;&#039;name&#039;]-&gt;me[&quot;string&quot;];<br />&nbsp; &nbsp; $tmp_price = $v-&gt;me&#91;&#039;struct&#039;]&#91;&#039;list_price&#039;]-&gt;me[&quot;double&quot;];<br />&nbsp; &nbsp; $out_array&#91;] = array(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;id&quot; =&gt; $tmp_id,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;name&quot; =&gt; $tmp_name,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;list_price&quot; =&gt; $tmp_price,<br />&nbsp; &nbsp; &nbsp; &nbsp; );<br />}<br />print_r($out_array);<br /><br />
    



    参考:
    [检测到链接无效,已移除]
    http://doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/index.html

    1 条回复 最后回复
    0
    • mrshellyM 离线
      mrshellyM 离线
      mrshelly
      写于 最后由 编辑
      #2

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

      1 条回复 最后回复
      0
      • 5 离线
        5 离线
        543070621
        写于 最后由 编辑
        #3

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

        1 条回复 最后回复
        0
        • F 离线
          F 离线
          fxiao
          写于 最后由 编辑
          #4

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

          1 条回复 最后回复
          0

          • 登录

          • 没有帐号? 注册

          • 登录或注册以进行搜索。
          • 第一个帖子
            最后一个帖子
          0
          • 版块
          • 标签
          • 热门
          • 用户
          • 群组