News.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Demo\Service;
  3. use Dever;
  4. class News
  5. {
  6. # 测试事务方式1:后缀为commit即可
  7. public function test_commit_api()
  8. {
  9. $cat = Dever::db('demo/cat');
  10. $data['name'] = '服饰' . time();
  11. $id[] = $cat->insert($data);
  12. $data['name'] = '潮流' . time();
  13. print_r($id);
  14. $data['id'] = 1;
  15. $id[] = $cat->insert($data);
  16. print_r($id);
  17. }
  18. # 测试事务方式2
  19. public function test_sw_api()
  20. {
  21. $cat = Dever::db('demo/cat');
  22. try {
  23. $cat->begin();
  24. $data['name'] = '服饰' . time();
  25. $id[] = $cat->insert($data);
  26. $data['name'] = '潮流' . time();
  27. $data['id'] = 1;
  28. $id[] = $cat->insert($data);
  29. $cat->commit();
  30. } catch (\Exception $e) {
  31. $cat->rollBack();
  32. }
  33. $data = $cat->getList();
  34. echo Dever::sql();
  35. return $data;
  36. }
  37. # 测试用dever::load直接调取url
  38. public function curl_api()
  39. {
  40. $data = Dever::load('http://www.vogue.com.cn/');
  41. print_r($data);die;
  42. }
  43. # 随便写一个数据
  44. public function data()
  45. {
  46. return array
  47. (
  48. 0 => array
  49. (
  50. 'id' => 1,
  51. 'name' => 't12',
  52. ),
  53. 1 => array
  54. (
  55. 'id' => 2,
  56. 'name' => 't22',
  57. )
  58. );
  59. }
  60. # api代理器,用于浏览器调用上述data
  61. # 访问方法:xx.demo.8dev.net/service/news.proxy?method=demo/service/news.data&test=1
  62. public function proxy_api()
  63. {
  64. $method = Dever::input('method');
  65. return Dever::load($method);
  66. }
  67. public function getData()
  68. {
  69. $config = array
  70. (
  71. 'id' => 1,
  72. 'type' => 3,
  73. 'data1' => array
  74. (
  75. 0 => array
  76. (
  77. 'id' => 1,
  78. 'name' => 't1',
  79. ),
  80. 1 => array
  81. (
  82. 'id' => 2,
  83. 'name' => 't2',
  84. )
  85. ),
  86. 'data2' => array
  87. (
  88. 0 => array
  89. (
  90. 'id' => 3,
  91. 'name' => 't3',
  92. ),
  93. 1 => array
  94. (
  95. 'id' => 4,
  96. 'name' => 't4',
  97. )
  98. ),
  99. );
  100. return $config;
  101. }
  102. public function get_api($callback)
  103. {
  104. //Dever::debug(11);
  105. return $callback;
  106. }
  107. public function get()
  108. {
  109. $param['option_title'] = array('邹游:最具学者胸怀和气质的设计师', 'like');
  110. $param['group'] = 'nid';
  111. $param['limit'] = '0,10';
  112. $data = Dever::db('demo/news')->all($param);
  113. return $data;
  114. }
  115. # 测试传统模板
  116. public function test_smarty_api()
  117. {
  118. $data['test'] = 1111111111111;
  119. $data['data'] = $this->test_foreach();
  120. //return $data;
  121. return Dever::render('smarty', $data);
  122. }
  123. public function test_foreach()
  124. {
  125. $config = array
  126. (
  127. array
  128. (
  129. 'id' => 1,
  130. 'name' => '1111',
  131. 'child' => array
  132. (
  133. array
  134. (
  135. 'id' => 3,
  136. 'name' => '3333',
  137. 'child' => array
  138. (
  139. array
  140. (
  141. 'id' => 9,
  142. 'name' => '9999',
  143. ),
  144. array
  145. (
  146. 'id' => 10,
  147. 'name' => '10101010',
  148. )
  149. ),
  150. ),
  151. array
  152. (
  153. 'id' => 4,
  154. 'name' => '4444',
  155. )
  156. ),
  157. ),
  158. array
  159. (
  160. 'id' => 2,
  161. 'name' => '2222',
  162. 'child' => array
  163. (
  164. array
  165. (
  166. 'id' => 5,
  167. 'name' => '5555',
  168. ),
  169. array
  170. (
  171. 'id' => 6,
  172. 'name' => '6666',
  173. )
  174. ),
  175. )
  176. );
  177. return $config;
  178. }
  179. }