ModelService.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <?php
  2. namespace Home\Service;
  3. /**
  4. * ModelService
  5. */
  6. class ModelService extends CommonService {
  7. /**
  8. * 得到数据模型的详细信息
  9. * @param array $where 查询条件
  10. * @param string $fields 插叙字段
  11. * @return array
  12. */
  13. public function getPagination($where, $fields,$order,$firstRow,$listRows) {
  14. $models = parent::getPagination($where,
  15. $fields,
  16. $order,
  17. $firstRow,
  18. $listRows);
  19. foreach ($models as $key => $model) {
  20. // 模型拥有的字段数目
  21. $models[$key]['fields_count'] = count($model['fields']);
  22. // 记录行数
  23. $rows = D('Common')->getTableRows($model['tbl_name']);
  24. $models[$key]['rows'] = $rows;
  25. }
  26. return $models;
  27. }
  28. /**
  29. * 得到所有的模型
  30. * @param string $fields 查询字段
  31. * @param string $order 排序
  32. * @return array
  33. */
  34. public function getAll($fields, $order) {
  35. return $this->getPagination(null, $fields, $order, null, null);
  36. }
  37. /**
  38. * 按id得到model数据
  39. * @param int $id
  40. * @return array
  41. */
  42. public function getById($id) {
  43. $model = $this->getD()->relation(true)->getById($id);
  44. if (empty($model)) {
  45. return null;
  46. }
  47. $model['fields_count'] = count($model['fields']);
  48. $model['rows'] = $this->getD()->getTableRows($model['tbl_name']);
  49. return $model;
  50. }
  51. /**
  52. * 添加模型并创建数据表
  53. * @param array $model
  54. * @return array
  55. */
  56. public function add($model) {
  57. $Model = $this->getD();
  58. $model = array_map('trim', $model);
  59. $model['tbl_name'] = C('DB_PREFIX') . $model['tbl_name'];
  60. $Model->startTrans();
  61. $model = $Model->create($model);
  62. $addStatus = $Model->add($model);
  63. // 创建数据表
  64. $createTblStatus = $Model->createTable($model['tbl_name'],
  65. $model['has_pk'],
  66. $model['tbl_engine'],
  67. $model['description']);
  68. // 添加系统字段
  69. $addFieldStatus = $this->addSystemFields($Model->getLastInsID());
  70. // 生成菜单项
  71. if (isset($model['is_inner']) && 0 == $model['is_inner']) {
  72. $this->addMenuItem($model);
  73. }
  74. // 生成节点
  75. $ctrlName = $this->getCtrlName($model['tbl_name']);
  76. $nodeService = D('Node', 'Service');
  77. $amns = $nodeService->addModuleNodes($model['menu_name'], $ctrlName);
  78. if (false === $addStatus
  79. || false === $createTblStatus
  80. || false === $addFieldStatus
  81. || false === $amns) {
  82. $Model->rollback();
  83. return $this->resultReturn(false);
  84. }
  85. $Model->commit();
  86. return $this->resultReturn(true);
  87. }
  88. /**
  89. * 添加旧的数据表
  90. *
  91. COLUMNS
  92. 当前数据库中当前用户可以访问的每一个列在该视图中占一行。INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects、spt_data type_info、systypes、syscolumns、syscomments、sysconfigures 以及 syscharsets 系统表为基础。
  93. 若要从这些视图中检索信息,请指定完全合格的 INFORMATION_SCHEMA view_name 名称。
  94. 列名 数据类型 描述
  95. TABLE_CATALOG nvarchar(128) 表限定符。
  96. TABLE_SCHEMA nvarchar(128) 表所有者。
  97. TABLE_NAME nvarchar(128) 表名。
  98. COLUMN_NAME nvarchar(128) 列名。
  99. ORDINAL_POSITION smallint 列标识号。
  100. COLUMN_DEFAULT nvarchar(4000) 列的默认值。
  101. IS_NULLABLE varchar(3) 列的为空性。如果列允许 NULL,那么该列返回 YES。否则,返回 NO。
  102. DATA_TYPE nvarchar(128) 系统提供的数据类型。
  103. CHARACTER_MAXIMUM_LENGTH smallint 以字符为单位的最大长度,适于二进制数据、字符数据,或者文本和图像数据。否则,返回 NULL。有关更多信息,请参见数据类型。
  104. CHARACTER_OCTET_LENGTH smallint 以字节为单位的最大长度,适于二进制数据、字符数据,或者文本和图像数据。否则,返回 NULL。
  105. NUMERIC_PRECISION tinyint 近似数字数据、精确数字数据、整型数据或货币数据的精度。否则,返回 NULL。
  106. NUMERIC_PRECISION_RADIX smallint 近似数字数据、精确数字数据、整型数据或货币数据的精度基数。否则,返回 NULL。
  107. NUMERIC_SCALE tinyint 近似数字数据、精确数字数据、整数数据或货币数据的小数位数。否则,返回 NULL。
  108. DATETIME_PRECISION smallint datetime 及 SQL-92 interval 数据类型的子类型代码。对于其它数据类型,返回 NULL。
  109. CHARACTER_SET_CATALOG varchar(6) 如果列是字符数据或 text 数据类型,那么返回 master,指明字符集所在的数据库。否则,返回 NULL。
  110. CHARACTER_SET_SCHEMA varchar(3) 如果列是字符数据或 text 数据类型,那么返回 DBO,指明字符集的所有者名称。否则,返回 NULL。
  111. CHARACTER_SET_NAME nvarchar(128) 如果该列是字符数据或 text 数据类型,那么为字符集返回唯一的名称。否则,返回 NULL。
  112. COLLATION_CATALOG varchar(6) 如果列是字符数据或 text 数据类型,那么返回 master,指明在其中定义排序次序的数据库。否则此列为 NULL。
  113. COLLATION_SCHEMA varchar(3) 返回 DBO,为字符数据或 text 数据类型指明排序次序的所有者。否则,返回 NULL。
  114. COLLATION_NAME nvarchar(128) 如果列是字符数据或 text 数据类型,那么为排序次序返回唯一的名称。否则,返回 NULL。
  115. DOMAIN_CATALOG nvarchar(128) 如果列是一种用户定义数据类型,那么该列是某个数据库名称,在该数据库名中创建了这种用户定义数据类型。否则,返回 NULL。
  116. DOMAIN_SCHEMA nvarchar(128) 如果列是一种用户定义数据类型,那么该列是这种用户定义数据类型的创建者。否则,返回 NULL。
  117. DOMAIN_NAME nvarchar(128) 如果列是一种用户定义数据类型,那么该列是这种用户定义数据类型的名称。否则,返回 NULL。
  118. * @param $model
  119. * @return array
  120. */
  121. public function add_old_table($model) {
  122. $Model = $this->getD();
  123. $model = array_map ( 'trim', $model );
  124. $new_db = $model ['db_name'];
  125. $new_table = $model ['tbl_name_old'];
  126. $model ['tbl_name'] = $model ['db_name'].'.'.$model ['tbl_name_old'];
  127. $Model->startTrans ();
  128. $model = $Model->create ( $model );
  129. $addStatus = $Model->add ( $model );
  130. $sql = "SELECT `COLUMN_NAME`,DATA_TYPE,COLUMN_COMMENT,COLUMN_TYPE FROM information_schema.columns WHERE table_schema ='".$new_db."' AND table_name = '".$new_table."'";
  131. $table_attribute = $Model->query ( $sql );
  132. $modelId = $Model->getLastInsID ();
  133. $fieldService = D ( 'Field', 'Service' );
  134. $inputService = D('input', 'Service');
  135. //查询此表主键
  136. $qu_key['name'] = $model ['tbl_name'];
  137. foreach ( $table_attribute as $ke => $val ) {
  138. $column_name = $val ['COLUMN_NAME'];
  139. $comment = $val ['COLUMN_COMMENT'];
  140. $model_name = $model ['tbl_name'];
  141. $length = "";
  142. if(preg_match('/\d+/',$val['COLUMN_TYPE'], $arr)){
  143. $length = $arr[0];
  144. }
  145. $field = array (
  146. 'model_id' => $modelId,
  147. 'name' => $column_name,
  148. 'comment' => $comment,
  149. 'type' => $val ['DATA_TYPE'],
  150. 'length'=> $length,
  151. 'is_requier' => 0,
  152. 'is_unique' => 0,
  153. 'is_index' => 0,
  154. 'is_system' => 0,
  155. 'auto_fill' =>'',
  156. 'created_at' => time (),
  157. 'updated_at' => time (),
  158. 'is_old' => 1
  159. );
  160. $addFieldStatus = $fieldService->just_add_field ( $field );
  161. $input = array (
  162. 'field_id' => $addFieldStatus ['data'] ['id'],
  163. 'is_show' => 1,
  164. 'label' => $column_name,
  165. 'remark' => $comment,
  166. 'type' => 'text',
  167. 'width' => 20,
  168. 'height' => 0,
  169. 'opt_value' => '',
  170. 'value' => '',
  171. 'editor' => 'all',
  172. 'html' => "<input type='text' class='input' size='20' name='".$model_name."[".$column_name."]' value='' />",
  173. 'show_order' => 1,
  174. 'created_at' => time (),
  175. 'updated_at' => time ()
  176. );
  177. $inputService->add ( $input );
  178. }
  179. // 生成菜单项
  180. if (isset($model['is_inner']) && 0 == $model['is_inner']) {
  181. $this->addMenuItem($model);
  182. }
  183. // 生成节点
  184. $ctrlName = $this->getCtrlName($model['tbl_name']);
  185. $nodeService = D('Node', 'Service');
  186. $amns = $nodeService->addModuleNodes($model['menu_name'], $ctrlName);
  187. if (false === $addStatus
  188. || false === $addFieldStatus
  189. || false === $amns) {
  190. $Model->rollback();
  191. return $this->resultReturn(false);
  192. }
  193. $Model->commit();
  194. return $this->resultReturn(true);
  195. }
  196. /**
  197. * 更新模型并更新数据表
  198. * @param array $model
  199. * @return array
  200. */
  201. public function update($model) {
  202. $Model = $this->getD();
  203. $model = array_map('trim', $model);
  204. $model['tbl_name'] = C('DB_PREFIX') . $model['tbl_name'];
  205. // 取出旧数据
  206. $old = $Model->field('tbl_name')->getById($model['id']);
  207. $Model->startTrans();
  208. $model = $Model->create($model);
  209. // 更新数据
  210. $updateStatus = $Model->save($model);
  211. // 更新数据表名
  212. if ($model['tbl_name'] != $old['tbl_name']) {
  213. $utnStatus = $Model->updateTableName($old['tbl_name'],
  214. $model['tbl_name']);
  215. }
  216. // 更新数据表注释
  217. if ($model['description'] != $old['description']) {
  218. $utcStatus = $Model->updateTableComment($model['tbl_name'],
  219. $model['description']);
  220. }
  221. // 更新菜单
  222. if (($model['menu_name'] != $old['menu_name']
  223. || $model['tbl_name'] != $old['tbl_name'])
  224. && 0 == $old['is_inner']) {
  225. $this->replaceMenuItem($model, $old);
  226. }
  227. if (false === $updateStatus
  228. || false === $utnStatus
  229. || false === $utcStatus) {
  230. $Model->rollback();
  231. // 撤回更新
  232. if (0 == $old['is_inner']) {
  233. $this->replaceMenuItem($old, $model);
  234. }
  235. return $this->resultReturn(false);
  236. }
  237. $Model->commit();
  238. return $this->resultReturn(true);
  239. }
  240. /**
  241. * 删除模型并且删除数据表
  242. * @param int $id 需要删除模型的id
  243. * @return boolean
  244. */
  245. public function delete($id) {
  246. $Model = $this->getD();
  247. $model = $Model->getById($id);
  248. if (empty($model)) {
  249. return $this->resultReturn(false);
  250. }
  251. $ctrlName = $this->getCtrlName($model['tbl_name']);
  252. $Model->startTrans();
  253. // 删除数据表
  254. $dropStatus = $Model->dropTable($model['tbl_name']);
  255. // 删除模型数据
  256. $delStatus = $Model->delete($id);
  257. // 删除菜单项
  258. $this->delMenuItem($ctrlName);
  259. // 删除节点
  260. D('Node', 'Service')->deleteModuleNodes($ctrlName);
  261. if (false === $dropStatus || false === $delStatus) {
  262. $Modle->rollback();
  263. // 还原菜单项
  264. if (0 == $model['is_inner']) {
  265. $this->addMenuItem($model);
  266. }
  267. return $this->resultReturn(false);
  268. }
  269. $Model->commit();
  270. return $this->resultReturn(true);
  271. }
  272. /**
  273. * 检查模型名称是否可用
  274. * @param string $name 模型名称
  275. * @param int $id 需要更新模型的id
  276. * @return array
  277. */
  278. public function checkModelName($name, $id) {
  279. $Model = $this->getD();
  280. $model['name'] = trim($name);
  281. if ($Model->isValidModelName($model, $id)) {
  282. return $this->resultReturn(true);
  283. }
  284. return $this->errorResultReturn($Model->getError());
  285. }
  286. /**
  287. * 检查数据表名称是否可用
  288. * @param string $name 数据表名称
  289. * @param int $id 需要更新模型的id
  290. * @return array
  291. */
  292. public function checkTblName($name, $id) {
  293. $Model = $this->getD();
  294. $model['tbl_name'] = trim($name);
  295. // 验证表名是否空
  296. if (empty($model['tbl_name'])) {
  297. return $this->errorResultReturn('数据表名不能为空');
  298. }
  299. // 验证表名是否已存在
  300. $model['tbl_name'] = C('DB_PREFIX') . $model['tbl_name'];
  301. if ($Model->isValidTblName($model, $id)) {
  302. return $this->resultReturn(true);
  303. }
  304. return $this->errorResultReturn($Model->getError());
  305. }
  306. /**
  307. * 检查菜单名称是否可用
  308. * @param string $name 菜单名称
  309. * @param int $id 需要更新模型的id
  310. * @return array
  311. */
  312. public function checkMenuName($name, $id) {
  313. $Model = $this->getD();
  314. $model['menu_name'] = trim($name);
  315. if ($Model->isValidMenuName($model, $id)) {
  316. return $this->resultReturn(true);
  317. }
  318. return $this->errorResultReturn($Model->getError());
  319. }
  320. /**
  321. * 检查模型是否可用
  322. * @param array $model 需要检查的模型
  323. * @param int $id 需要更新模型的id
  324. * @return array
  325. */
  326. public function checkModel($model, $id) {
  327. $Model = $this->getD();
  328. // 检查表名是否合法
  329. $model = array_map('trim', $model);
  330. $resutl = $this->checkTblName($model['tbl_name'], $id);
  331. // // 需要检查的模型id
  332. // $_SESSION['update_id'] = $model['id'];
  333. if (false === $resutl['status']) {
  334. return $this->errorResultReturn($resutl['data']['error']);
  335. }
  336. if ($Model->isValid($model, $id)) {
  337. return $this->resultReturn(true);
  338. }
  339. return $this->errorResultReturn($Model->getError());
  340. }
  341. /**
  342. * 检查模型是否存在
  343. * @param int $modeId
  344. * @return boolean
  345. */
  346. public function existModel($modeId) {
  347. if ($this->getM()->where("id = {$modeId}")->count() > 0) {
  348. return true;
  349. }
  350. return false;
  351. }
  352. /**
  353. * 检查模型是否存在指定的字段
  354. * @param int $modelId 模型id
  355. * @param int $fieldId 字段id
  356. * @return boolean
  357. */
  358. public function hasField($modelId, $fieldId) {
  359. $where = array('model_id' => $modelId, 'id' => $fieldId);
  360. if (null == M('Field')->where($where)->find()) {
  361. return false;
  362. }
  363. return true;
  364. }
  365. /**
  366. * 添加系统字段:id、created_at、updated_at
  367. * @param int $modelId 模型id
  368. * @return boolean 是否添加成功
  369. */
  370. private function addSystemFields($modelId) {
  371. $Field = D('Field');
  372. // id字段
  373. $id = array('model_id' => $modelId,
  374. 'name' => 'id',
  375. 'comment' => '表主键',
  376. 'type' => 'INT',
  377. 'is_requier' => 1,
  378. 'is_unique' => 1,
  379. 'is_index' => 1,
  380. 'is_system' => 1);
  381. $id = $Field->create($id);
  382. $status = false !== $Field->add($id) ? true : false;
  383. // created_at updated_at
  384. $timestamp = array('model_id' => $modelId,
  385. 'type' => 'INT',
  386. 'is_index' => 1,
  387. 'is_system' => 1,
  388. 'auto_fill' => 'time');
  389. $timestamp = $Field->create($timestamp);
  390. // created_at字段
  391. $timestamp['name'] = 'created_at';
  392. $timestamp['comment'] = '创建时间';
  393. $timestamp['fill_time'] = 'insert';
  394. $status = false !== $Field->add($timestamp) ? true : false;
  395. // updated_at字段
  396. $timestamp['name'] = 'updated_at';
  397. $timestamp['comment'] = '更新时间';
  398. $timestamp['fill_time'] = 'both';
  399. $status = false !== $Field->add($timestamp) ? true : false;
  400. return $status;
  401. }
  402. /**
  403. * 添加菜单项
  404. * @param array $model
  405. */
  406. private function addMenuItem(array $model) {
  407. $modelLogic = D('Model', 'Logic');
  408. // 得到模型的控制器名称
  409. $ctrlName = $this->getCtrlName($model['tbl_name']);
  410. // 生成菜单项
  411. $item = $modelLogic->genMenuItem($model['menu_name'], $ctrlName);
  412. // 添加菜单项
  413. $menu = $modelLogic->addMenuItem($item);
  414. }
  415. /**
  416. * 删除菜单项
  417. * @param string $ctrlName 菜单项对应的控制器名称
  418. * @return mixed
  419. */
  420. private function delMenuItem($ctrlName) {
  421. return D('Model', 'Logic')->delMenuItem($ctrlName);
  422. }
  423. /**
  424. * 替换菜单项
  425. * @param array $model
  426. * @param array $old
  427. * @return array
  428. */
  429. private function replaceMenuItem($model, $old) {
  430. $modelLogic = D('Model', 'Logic');
  431. $oldCtrlName = $this->getCtrlName($old['tbl_name']);
  432. $ctrlName = $this->getCtrlName($model['tbl_name']);
  433. // 生成新菜单项
  434. $item = $modelLogic->genMenuItem($model['menu_name'], $ctrlName);
  435. // 替换旧的菜单项
  436. return $modelLogic->replaceMenuItem($item, $oldCtrlName);
  437. }
  438. /**
  439. * 以数据表名得到控制器名称
  440. * @param string $tblName
  441. * @return string
  442. */
  443. public function getCtrlName($tblName) {
  444. if (strpos($tblName, '.') === false) {
  445. // 去掉表前缀
  446. $tblName = substr($tblName, strpos($tblName, '_') + 1);
  447. $tblName = str_replace('_', ' ', $tblName);
  448. // 单词首字母转为大写
  449. $tblName = ucwords($tblName);
  450. $tblName = str_replace(' ', '', $tblName);
  451. }
  452. return $tblName;
  453. }
  454. /**
  455. * 以控制器名得到表名称
  456. * @param string $ctrlName 控制器名
  457. * @return string
  458. */
  459. public function getTblName($ctrlName) {
  460. $tblName = '';
  461. $ctrArr = explode('.', $ctrlName);
  462. if (strpos($ctrlName, '.') === false || (strpos($ctrlName, '.') && $ctrArr[0] == C('DB_NAME') && $ctrlName = $ctrArr[1])) {
  463. for ($i = 0, $len = strlen($ctrlName); $i < $len; $i++) {
  464. if (strtoupper($ctrlName[$i]) === $ctrlName[$i]) {
  465. // 大写字母
  466. $tblName .= '_' . strtolower($ctrlName[$i]);
  467. continue ;
  468. }
  469. $tblName .= $ctrlName[$i];
  470. }
  471. $tblName = C('DB_PREFIX') . substr($tblName, 1);
  472. } else {
  473. $tblName = $ctrlName;
  474. }
  475. return $tblName;
  476. }
  477. protected function getModelName() {
  478. return 'Model';
  479. }
  480. public function diff_table($model_id, $tbl_name, $sync = false) {
  481. $m = $this->getM();
  482. $db_name = C("DB_NAME");
  483. $tmp_name = $tbl_name;
  484. if(strpos($tbl_name, ".") !== false) {
  485. $tmp = explode(".", $tbl_name);
  486. $db_name = $tmp[0];
  487. $tbl_name = $tmp[1];
  488. }
  489. $model_field = $m->query("select name,type,length,value,comment,id from ea_field where model_id= $model_id");
  490. $table_field = $m->query("SELECT `COLUMN_NAME`,DATA_TYPE,COLUMN_TYPE,COLUMN_COMMENT FROM information_schema.columns WHERE table_schema ='".$db_name."' AND table_name = '".$tbl_name."'" );
  491. $result = [];
  492. $field_names = [];
  493. $diff_result = [];
  494. if($table_field) {
  495. foreach ( $table_field as $tk => $tv) {
  496. $length = "";
  497. if(preg_match('/\d+/',$tv['COLUMN_TYPE'], $arr)){
  498. $length = $arr[0];
  499. }
  500. $tmp = [
  501. 'name'=>$tv['COLUMN_NAME'],
  502. 'type'=>$tv['DATA_TYPE'],
  503. 'length'=>$length,
  504. 'comment'=>$tv['COLUMN_COMMENT']
  505. ];
  506. $field_names[] = $tmp['name'];
  507. $result[$tmp['name']] = $tmp;
  508. }
  509. }
  510. if($model_field) {
  511. foreach ( $model_field as $mk => $mv ) {
  512. if( !in_array($mv['name'], $field_names)) {
  513. } else {
  514. $tmp = $result[$mv['name']];
  515. $tmp['id'] = $mv['id'];
  516. unset($result[$mv['name']]);
  517. foreach($tmp as $k=>$v) {
  518. if(strtolower($mv[$k]) != strtolower($v)) {
  519. unset($mv['value']);
  520. $diff_result['diff'][$mv['name']] = [$tmp, $mv];
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. if($result) {
  528. $diff_result['new'] = $result;
  529. }
  530. if($sync) {
  531. foreach($diff_result['new'] as $dv) {
  532. $r = $this->update_field(0, $model_id, $tmp_name, $dv['name'], $dv['comment'], $dv['type'], $dv['length']);
  533. }
  534. foreach($diff_result['diff'] as $dv) {
  535. $dv = $dv[0];
  536. $r = $this->update_field($dv['id'], $model_id, $tmp_name, $dv['name'], $dv['comment'], $dv['type'], $dv['length']);
  537. }
  538. return true;
  539. }
  540. return $diff_result;
  541. }
  542. protected function update_field($field_id, $model_id, $tablename, $name, $comment, $type, $len) {
  543. //获取目标表字段到本库
  544. $fieldService = D ( 'Field', 'Service' );
  545. $inputService = D('input', 'Service');
  546. $field = array (
  547. 'model_id' => $model_id,
  548. 'name' => $name,
  549. 'comment' => $comment,
  550. 'type' => $type,
  551. 'length'=> $len,
  552. 'is_requier' => 0,
  553. 'is_unique' => 0,
  554. 'is_index' => 0,
  555. 'is_system' => 0,
  556. 'created_at' => time(),
  557. 'updated_at' => time(),
  558. 'is_old' => 1
  559. );
  560. $is_update = false;
  561. if($field_id) {
  562. $is_update = true;
  563. $field['id'] = $field_id;
  564. $retField = $fieldService->update($field);
  565. }else {
  566. $retField = $fieldService->add($field);
  567. $field_id = $retField['data'] ['id'];
  568. }
  569. //插入input
  570. $input = array (
  571. 'field_id' => $field_id,
  572. 'is_show' => 1,
  573. 'label' => $comment,
  574. 'remark' => $comment,
  575. 'type' => 'text',
  576. 'width' => 20,
  577. 'height' => 0,
  578. 'opt_value' => '',
  579. 'value' => '',
  580. 'editor' => 'all',
  581. 'html' => "<input type='text' class='input' size='20' name=".$tablename."[{$name}]' value='' />",
  582. 'show_order' => 1,
  583. 'created_at' => time (),
  584. 'updated_at' => time ()
  585. );
  586. if($is_update) {
  587. $id = M('Input')->where(['field_id' => $field_id])->getField('id');
  588. $input['id'] = $id;
  589. $retInput = $inputService->update($input);
  590. } else {
  591. $retInput = $inputService->add($input);
  592. }
  593. if($retField['status'] && $retInput['status']) {
  594. return true;
  595. } else {
  596. return false;
  597. }
  598. }
  599. }