SystemCrudServices.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\dao\system\SystemCrudDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\crud\Controller;
  16. use crmeb\services\crud\Dao;
  17. use crmeb\services\crud\enum\FormTypeEnum;
  18. use crmeb\services\crud\enum\SearchEnum;
  19. use crmeb\services\crud\Make;
  20. use crmeb\services\crud\Model;
  21. use crmeb\services\crud\Route;
  22. use crmeb\services\crud\Service;
  23. use crmeb\services\crud\Validate;
  24. use crmeb\services\crud\ViewApi;
  25. use crmeb\services\crud\ViewPages;
  26. use crmeb\services\crud\ViewRouter;
  27. use crmeb\services\FileService;
  28. use Phinx\Db\Adapter\AdapterFactory;
  29. use think\facade\Db;
  30. use think\helper\Str;
  31. use think\migration\db\Column;
  32. use think\migration\db\Table;
  33. /**
  34. * Class SystemCrudServices
  35. * @author 等风来
  36. * @email 136327134@qq.com
  37. * @date 2023/4/6
  38. * @package app\services\system
  39. */
  40. class SystemCrudServices extends BaseServices
  41. {
  42. //不能生成的系统自带表
  43. const NOT_CRUD_TABANAME = [
  44. 'system_config', 'system_attachment', 'system_attachment_category', 'system_config_tab',
  45. 'system_admin', 'eb_system_city', 'system_log', 'system_menus', 'system_notice',
  46. 'system_notice_admin', 'system_notification', 'system_role', 'system_route',
  47. 'system_route_cate', 'system_storage', 'system_timer', 'system_user_level',
  48. 'system_crud', 'wechat_key', 'user_label_relation', 'user_brokerage_frozen',
  49. 'user_brokerage', 'store_product_cate', 'store_bargain_user_help', 'shipping_templates_region',
  50. 'shipping_templates_no_delivery', 'shipping_templates_free', 'other_order_status', 'lang_code',
  51. 'lang_country', 'app_version', 'user', 'wechat_user', 'template_message', 'store_order',
  52. 'other_order', 'store_order_cart_info', 'store_order_economize', 'store_order_invoice', 'store_order_refund',
  53. 'store_order_status', 'store_pink', 'agent_level', 'agent_level_task', 'agent_level_task_record',
  54. 'agreement', 'app_version', 'article', 'article_category', 'article_content', 'auxiliary', 'cache',
  55. 'capital_flow', 'category', 'diy', 'express', 'lang_type', 'live_anchor', 'live_goods', 'live_room',
  56. 'live_room_goods', 'luck_lottery', 'luck_lottery_record', 'luck_prize', 'member_card',
  57. 'member_card_batch', 'member_right', 'member_ship', 'message_system', 'other_order',
  58. 'other_order_status', 'out_account', 'out_interface', 'page_categroy', 'page_link', 'qrcode',
  59. 'shipping_templates', 'shipping_templates_free', 'shipping_templates_no_delivery',
  60. 'shipping_templates_region', 'sms_record', 'store_advance', 'store_bargain', 'store_bargain_user',
  61. 'store_bargain_user_help', 'store_cart', 'store_category', 'store_combination', 'store_coupon_issue',
  62. 'store_coupon_issue_user', 'store_coupon_product', 'store_coupon_user', 'store_integral',
  63. 'store_integral_order', 'store_integral_order_status', 'store_order', 'store_order_cart_info',
  64. 'store_order_economize', 'store_order_invoice', 'store_order_refund', 'store_order_status',
  65. 'store_pink', 'store_product', 'store_product_attr', 'store_product_attr_result',
  66. 'store_product_attr_value', 'store_product_cate', 'store_product_coupon', 'store_product_description',
  67. 'store_product_log', 'store_product_relation', 'store_service', 'store_service_feedback',
  68. 'store_product_reply', 'store_product_rule', 'store_product_virtual', 'store_seckill', 'store_seckill_time',
  69. 'store_service_log', 'store_service_record', 'store_service_speechcraft', 'store_visit',
  70. 'system_attachment', 'system_attachment_category', 'system_city', 'system_config',
  71. 'system_config_tab', 'system_file', 'system_file_info', 'system_group', 'system_group_data',
  72. 'system_log', 'system_notice', 'system_notice_admin', 'system_notification',
  73. 'system_role', 'system_route', 'system_route_cate', 'system_storage', 'system_store',
  74. 'system_store_staff', 'system_timer', 'system_user_level', 'template_message', 'upgrade_log',
  75. 'user', 'user_address', 'user_bill', 'user_brokerage', 'user_brokerage_frozen', 'user_cancel',
  76. 'user_enter', 'user_extract', 'user_friends', 'user_group', 'user_invoice', 'user_label',
  77. 'user_label_relation', 'user_level', 'user_money', 'user_notice', 'user_notice_see',
  78. 'user_recharge', 'user_search', 'user_sign', 'user_spread', 'user_visit', 'wechat_key',
  79. 'wechat_media', 'wechat_message', 'wechat_news_category', 'wechat_qrcode', 'wechat_qrcode_cate',
  80. 'wechat_qrcode_record', 'wechat_reply', 'wechat_user', 'system_crud_data', 'admins',
  81. ];
  82. //表字符集
  83. const TABLR_COLLATION = 'utf8mb4_general_ci';
  84. /**
  85. * SystemCrudServices constructor.
  86. * @param SystemCrudDao $dao
  87. */
  88. public function __construct(SystemCrudDao $dao)
  89. {
  90. $this->dao = $dao;
  91. }
  92. /**
  93. * @return array
  94. * @author 等风来
  95. * @email 136327134@qq.com
  96. * @date 2023/4/11
  97. */
  98. public function getList()
  99. {
  100. [$page, $limit] = $this->getPageValue();
  101. $list = $this->dao->selectList([], 'add_time,id,name,table_name,table_comment,table_collation', $page, $limit, 'id desc');
  102. $count = $this->dao->count();
  103. return compact('list', 'count');
  104. }
  105. /**
  106. * 数据库字段类型
  107. * @return \string[][]
  108. * @author 等风来
  109. * @email 136327134@qq.com
  110. * @date 2023/4/11
  111. */
  112. public function getTabelRule()
  113. {
  114. $rule = [
  115. 'varchar' => 'string',
  116. 'int' => 'integer',
  117. 'biginteger' => 'bigint',
  118. 'tinyint' => 'boolean',
  119. ];
  120. return [
  121. 'types' => [
  122. 'varchar',
  123. 'char',
  124. 'text',
  125. 'longtext',
  126. 'tinytext',
  127. 'enum',
  128. 'blob',
  129. 'binary',
  130. 'varbinary',
  131. 'datetime',
  132. 'timestamp',
  133. 'time',
  134. 'date',
  135. 'year',
  136. 'boolean',
  137. 'tinyint',
  138. 'int',
  139. 'decimal',
  140. 'float',
  141. 'json',
  142. ],
  143. 'form' => [
  144. [
  145. 'value' => FormTypeEnum::INPUT,
  146. 'label' => '输入框',
  147. 'field_type' => 'varchar',
  148. 'limit' => 255
  149. ],
  150. [
  151. 'value' => FormTypeEnum::NUMBER,
  152. 'label' => '数字输入框',
  153. 'field_type' => 'int',
  154. 'limit' => 11
  155. ],
  156. [
  157. 'value' => FormTypeEnum::TEXTAREA,
  158. 'label' => '多行文本框',
  159. 'field_type' => 'text',
  160. 'limit' => null
  161. ],
  162. [
  163. 'value' => FormTypeEnum::DATE_TIME,
  164. 'label' => '单选日期时间',
  165. 'field_type' => 'varchar',
  166. 'limit' => 200
  167. ],
  168. [
  169. 'value' => FormTypeEnum::DATE_TIME_RANGE,
  170. 'label' => '日期时间区间选择',
  171. 'field_type' => 'varchar',
  172. 'limit' => 200
  173. ],
  174. [
  175. 'value' => FormTypeEnum::CHECKBOX,
  176. 'label' => '多选框',
  177. 'field_type' => 'varchar',
  178. 'limit' => 200
  179. ],
  180. [
  181. 'value' => FormTypeEnum::RADIO,
  182. 'label' => '单选框',
  183. 'field_type' => 'int',
  184. 'limit' => 11
  185. ],
  186. [
  187. 'value' => FormTypeEnum::SWITCH,
  188. 'label' => '开关',
  189. 'field_type' => 'int',
  190. 'limit' => 11
  191. ],
  192. [
  193. 'value' => FormTypeEnum::SELECT,
  194. 'label' => '下拉框',
  195. 'field_type' => 'int',
  196. 'limit' => 11
  197. ],
  198. [
  199. 'value' => FormTypeEnum::FRAME_IMAGE_ONE,
  200. 'label' => '单图选择',
  201. 'field_type' => 'varchar',
  202. 'limit' => 200
  203. ],
  204. [
  205. 'value' => FormTypeEnum::FRAME_IMAGES,
  206. 'label' => '多图选择',
  207. 'field_type' => 'varchar',
  208. 'limit' => 200
  209. ],
  210. ],
  211. 'search_type' => [
  212. [
  213. 'value' => SearchEnum::SEARCH_TYPE_EQ,
  214. 'label' => '等于搜索',
  215. ],
  216. [
  217. 'value' => SearchEnum::SEARCH_TYPE_LTEQ,
  218. 'label' => '小于等于搜索',
  219. ],
  220. [
  221. 'value' => SearchEnum::SEARCH_TYPE_GTEQ,
  222. 'label' => '大于等于搜索',
  223. ],
  224. [
  225. 'value' => SearchEnum::SEARCH_TYPE_NEQ,
  226. 'label' => '不等于搜索',
  227. ],
  228. [
  229. 'value' => SearchEnum::SEARCH_TYPE_LIKE,
  230. 'label' => '模糊搜索',
  231. ],
  232. [
  233. 'value' => SearchEnum::SEARCH_TYPE_BETWEEN,
  234. 'label' => '用来时间区间搜索',
  235. ],
  236. ],
  237. 'default_type' => [
  238. [
  239. 'value' => '-1',
  240. 'label' => '无',
  241. 'disabled' => false,
  242. ],
  243. [
  244. 'value' => '1',
  245. 'label' => '自定义',
  246. 'disabled' => true,
  247. ],
  248. [
  249. 'value' => '2',
  250. 'label' => 'NULL',
  251. 'disabled' => false,
  252. ],
  253. [
  254. 'value' => '3',
  255. 'label' => 'CURRENT_TIMESTAMP',
  256. 'disabled' => false,
  257. ],
  258. ],
  259. 'rule' => $rule
  260. ];
  261. }
  262. /**
  263. * 改变数据库类型
  264. * @param string $type
  265. * @return string
  266. * @author 等风来
  267. * @email 136327134@qq.com
  268. * @date 2023/4/13
  269. */
  270. public function changeTabelRule(string $type)
  271. {
  272. if (!in_array($type, $this->getTabelRule()['types'])) {
  273. throw new AdminException('字段类型不存在');
  274. }
  275. return $this->getTabelRule()['rule'][$type] ?? $type;
  276. }
  277. /**
  278. * @param string $tableName
  279. * @return mixed
  280. * @author 等风来
  281. * @email 136327134@qq.com
  282. * @date 2023/4/14
  283. */
  284. public function getTableInfo(string $tableName)
  285. {
  286. $sql = 'SELECT * FROM `information_schema`.`TABLES` WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?';
  287. $tableInfo = Db::query($sql, [config('database.connections.mysql.database'), $this->getTableName($tableName)]);
  288. return $tableInfo[0] ?? [];
  289. }
  290. /**
  291. * 获取表字段
  292. * @param string $tableName
  293. * @return mixed
  294. * @author 等风来
  295. * @email 136327134@qq.com
  296. * @date 2023/4/7
  297. */
  298. public function getColumnNamesList(string $tableName)
  299. {
  300. $sql = 'SELECT * FROM `information_schema`.`columns` WHERE TABLE_SCHEMA = ? AND table_name = ? ORDER BY ORDINAL_POSITION';
  301. $column = Db::query($sql, [config('database.connections.mysql.database'), $this->getTableName($tableName)]);
  302. $columns = [];
  303. foreach ($column as $item) {
  304. $column = [
  305. 'name' => $item['COLUMN_NAME'],
  306. 'type' => $item['DATA_TYPE'],
  307. 'dataType' => stripos($item['COLUMN_TYPE'], '(') !== false ? substr_replace($item['COLUMN_TYPE'], '', stripos($item['COLUMN_TYPE'], ')') + 1) : $item['COLUMN_TYPE'],
  308. 'default' => $item['COLUMN_DEFAULT'],
  309. 'null' => $item['IS_NULLABLE'] == 'YES',
  310. 'primaryKey' => $item['COLUMN_KEY'] == 'PRI',
  311. 'unsigned' => (bool)stripos($item['COLUMN_TYPE'], 'unsigned'),
  312. 'autoIncrement' => stripos($item['EXTRA'], 'auto_increment') !== false,
  313. 'comment' => $item['COLUMN_COMMENT'],
  314. 'limit' => $item['CHARACTER_MAXIMUM_LENGTH'] ?: $item['NUMERIC_PRECISION'],
  315. ];
  316. $columns[$item['COLUMN_NAME']] = $column;
  317. }
  318. return $columns;
  319. }
  320. /**
  321. * 获取当前数据库所有表名
  322. * @return mixed
  323. * @author 等风来
  324. * @email 136327134@qq.com
  325. * @date 2023/8/2
  326. */
  327. public function getTableAll()
  328. {
  329. $sql = "SELECT TABLE_NAME, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?";
  330. $tableAll = Db::query($sql, [config('database.connections.mysql.database')]);
  331. $data = [];
  332. foreach ($tableAll as $item) {
  333. $item['TABLE_NAME'] = str_replace(config('database.connections.mysql.prefix'), '', $item['TABLE_NAME']);
  334. // if (!in_array($item['TABLE_NAME'], self::NOT_CRUD_TABANAME)) {
  335. $data[] = [
  336. 'value' => $item['TABLE_NAME'],
  337. 'label' => $item['TABLE_COMMENT'] ?: $item['TABLE_NAME'],
  338. ];
  339. // }
  340. }
  341. return $data;
  342. }
  343. /**
  344. * @param array $data
  345. * @return array
  346. * @author 等风来
  347. * @email 136327134@qq.com
  348. * @date 2023/4/12
  349. */
  350. public function valueReplace(array $data)
  351. {
  352. $replace = ['phar://'];
  353. $newData = [];
  354. foreach ($data as $key => $item) {
  355. if (is_array($item)) {
  356. $item = $this->valueReplace($item);
  357. } else {
  358. $item = str_replace($replace, '', $item);
  359. }
  360. $newData[str_replace($replace, '', $key)] = $item;
  361. }
  362. return $newData;
  363. }
  364. /**
  365. * 更新表字段
  366. * @param string $tableName
  367. * @param string $field
  368. * @param string $changeFiled
  369. * @param string $type
  370. * @param string $limit
  371. * @param string $default
  372. * @param string $comment
  373. * @param array $options
  374. * @return mixed
  375. * @author 等风来
  376. * @email 136327134@qq.com
  377. * @date 2023/4/24
  378. */
  379. protected function updateAlter(string $tableName, string $field, string $changeFiled, string $prevFiled, string $type, $limit = '', string $default = '', string $comment = '', array $options = [])
  380. {
  381. $tableName = $this->getTableName($tableName);
  382. $comment = addslashes($comment);
  383. $field = addslashes($field);
  384. $changeFiled = addslashes($changeFiled);
  385. $prevFiled = addslashes($prevFiled);
  386. $type = addslashes($type);
  387. $default = addslashes($default);
  388. if ($prevFiled) {
  389. $after = "AFTER `$prevFiled`";
  390. } else {
  391. $after = "";
  392. }
  393. if (isset($options['default_type'])) {
  394. switch ($options['default_type']) {
  395. case '-1':
  396. $default = 'NULL';
  397. break;
  398. case '1'://自定义
  399. $default = "NOT NULL DEFAULT '$default'";
  400. break;
  401. case '2'://为null
  402. $default = 'NULL DEFAULT NULL';
  403. break;
  404. case '3'://时间
  405. $default = 'NULL DEFAULT CURRENT_TIMESTAMP';
  406. break;
  407. }
  408. }
  409. if (in_array(strtolower($type), ['text', 'longtext', 'tinytext'])) {
  410. $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type CHARACTER SET utf8mb4 COLLATE " . self::TABLR_COLLATION . " NULL COMMENT '$comment' $after;";
  411. } else if (strtolower($type) == 'enum') {
  412. $enum = [];
  413. foreach ($options['options'] as $option) {
  414. $enum[] = "'$option'";
  415. }
  416. $enumStr = implode(',', $enum);
  417. $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type($enumStr) $default COMMENT '$comment' $after;";
  418. } else {
  419. $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type($limit) $default COMMENT '$comment' $after;";
  420. }
  421. return Db::execute($sql);
  422. }
  423. /**
  424. * 添加字段
  425. * @param string $tableName
  426. * @param string $field
  427. * @param string $prevFiled
  428. * @param string $type
  429. * @param string $limit
  430. * @param string $default
  431. * @param string $comment
  432. * @param array $options
  433. * @return mixed
  434. * @author 等风来
  435. * @email 136327134@qq.com
  436. * @date 2023/4/24
  437. */
  438. public function addAlter(string $tableName, string $field, string $prevFiled, string $type, $limit = '', string $default = '', string $comment = '', array $options = [])
  439. {
  440. $tableName = $this->getTableName($tableName);
  441. $comment = addslashes($comment);
  442. $field = addslashes($field);
  443. $prevFiled = addslashes($prevFiled);
  444. $type = addslashes($type);
  445. $default = addslashes($default);
  446. if ($prevFiled) {
  447. $after = "AFTER `$prevFiled`";
  448. } else {
  449. $after = "";
  450. }
  451. if (isset($options['default_type'])) {
  452. switch ($options['default_type']) {
  453. case '-1':
  454. $default = 'NULL';
  455. break;
  456. case '1'://自定义
  457. $default = "NOT NULL DEFAULT '$default'";
  458. break;
  459. case '2'://为null
  460. $default = 'NULL DEFAULT NULL';
  461. break;
  462. case '3'://时间
  463. $default = 'NULL DEFAULT CURRENT_TIMESTAMP';
  464. break;
  465. }
  466. }
  467. if (in_array(strtolower($type), ['text', 'longtext', 'tinytext'])) {
  468. $sql = "ALTER TABLE `$tableName` ADD `$field` $type NULL COMMENT '$comment' $after;";
  469. } else {
  470. $defaultSql = $default;
  471. //处理时间字段默认值
  472. if (in_array(strtolower($type), ['datetime', 'timestamp', 'time', 'date', 'year'])) {
  473. switch ($field) {
  474. case 'delete_time':
  475. $defaultSql = 'NULL DEFAULT NULL';
  476. break;
  477. case 'create_time':
  478. case 'update_time':
  479. $defaultSql = 'NOT NULL DEFAULT CURRENT_TIMESTAMP';
  480. break;
  481. }
  482. }
  483. //兼容枚举字段
  484. if (strtolower($type) == 'enum') {
  485. $enum = [];
  486. foreach ($options['options'] as $option) {
  487. $enum[] = "'$option'";
  488. }
  489. $enumStr = implode(',', $enum);
  490. $limitSql = $enumStr ? '(' . $enumStr . ')' : '';
  491. } else {
  492. $limitSql = $limit ? '(' . $limit . ')' : '';
  493. }
  494. $sql = "ALTER TABLE `$tableName` ADD `$field` $type$limitSql $defaultSql COMMENT '$comment' $after;";
  495. }
  496. return Db::execute($sql);
  497. }
  498. /**
  499. * 删除表字段
  500. * @param string $tableName
  501. * @param string $field
  502. * @return mixed
  503. * @author 等风来
  504. * @email 136327134@qq.com
  505. * @date 2023/4/24
  506. */
  507. protected function deleteAlter(string $tableName, string $field)
  508. {
  509. $tableName = $this->getTableName($tableName);
  510. $field = addslashes($field);
  511. $sql = "ALTER TABLE `$tableName` DROP `$field`";
  512. return Db::execute($sql);
  513. }
  514. /**
  515. * 修改表备注
  516. * @param string $tableName
  517. * @param string $common
  518. * @return mixed
  519. * @author 等风来
  520. * @email 136327134@qq.com
  521. * @date 2023/4/24
  522. */
  523. protected function updateFromCommon(string $tableName, string $common)
  524. {
  525. $tableName = $this->getTableName($tableName);
  526. $common = addslashes($common);
  527. $sql = "ALTER TABLE `$tableName` COMMENT = '$common';";
  528. return Db::execute($sql);
  529. }
  530. /**
  531. * 对比字段变动了更改
  532. * @param string $tableName
  533. * @param array $deleteField
  534. * @param array $tableField
  535. * @author 等风来
  536. * @email 136327134@qq.com
  537. * @date 2023/4/24
  538. */
  539. protected function diffAlter(string $tableName, array $deleteField, array $tableField)
  540. {
  541. $updateAlter = [];
  542. $addAlter = [];
  543. $columns = $this->getColumnNamesList($tableName);
  544. $fieldAll = array_column($columns, 'name');
  545. //对比数据库字段
  546. foreach ($tableField as $i => $item) {
  547. if ($item['primaryKey'] || $item['field'] == 'delete_time') {
  548. continue;
  549. }
  550. $prevFiled = $i ? ($tableField[$i - 1]['field'] ?? 'id') : 'id';
  551. //前台新增的字段进行添加
  552. if (!(isset($item['default_field']) &&
  553. isset($item['default_field_type']) &&
  554. isset($item['default_limit']) &&
  555. isset($item['default_comment']) &&
  556. isset($item['default_default']) &&
  557. isset($item['default_default_type']))
  558. ) {
  559. if (!in_array($item['field'], $fieldAll)) {
  560. $addAlter[] = [
  561. 'prev_filed' => $prevFiled,
  562. 'field' => $item['field'],
  563. 'limit' => $item['limit'],
  564. 'type' => $item['field_type'],
  565. 'comment' => $item['comment'],
  566. 'default' => $item['default'],
  567. 'default_type' => $item['default_type'],
  568. 'values' => $item['field_type'] == 'enum' ? $item['limit'] : [],
  569. ];
  570. }
  571. continue;
  572. } else {
  573. //从数据库中新增的字段,并没有记录在表中做兼容处理;
  574. //默认字段没有在数据库中,需要添加字段;
  575. if (!in_array($item['default_field'], $fieldAll)) {
  576. $addAlter[] = [
  577. 'prev_filed' => $prevFiled,
  578. 'field' => $item['field'],
  579. 'limit' => $item['limit'],
  580. 'type' => $item['field_type'],
  581. 'comment' => $item['comment'],
  582. 'default' => $item['default'],
  583. 'default_type' => $item['default_type'],
  584. 'values' => $item['field_type'] == 'enum' ? $item['limit'] : [],
  585. ];
  586. continue;
  587. }
  588. }
  589. if ($item['default_field'] != $item['field'] && in_array($item['field_type'], ['addTimestamps', 'addSoftDelete'])) {
  590. throw new AdminException($item['field'] . '字段不允许被更改');
  591. }
  592. //数据库表存在的,字段,并且被修改
  593. if (!in_array($item['field'], ['id', 'create_time', 'update_time'])) {
  594. $updateAlter[] = [
  595. 'default_field' => $item['default_field'],
  596. 'prev_filed' => $prevFiled,
  597. 'field' => $item['field'],
  598. 'limit' => $item['limit'],
  599. 'type' => $item['field_type'],
  600. 'comment' => $item['comment'],
  601. 'default' => $item['default'],
  602. 'default_type' => $item['default_type'],
  603. 'values' => $item['field_type'] == 'enum' ? $item['limit'] : [],
  604. ];
  605. }
  606. }
  607. //添加字段
  608. foreach ($addAlter as $item) {
  609. $this->addAlter($tableName, $item['field'], $item['prev_filed'], $item['type'], $item['limit'], $item['default'], $item['comment'], [
  610. 'options' => $item['values'],
  611. 'default_type' => $item['default_type'],
  612. ]);
  613. }
  614. //删除多余字段
  615. foreach ($deleteField as $item) {
  616. $this->deleteAlter($tableName, $item);
  617. }
  618. //更新数据库字段
  619. foreach ($updateAlter as $item) {
  620. $this->updateAlter($tableName, $item['default_field'], $item['field'], $item['prev_filed'], $item['type'], $item['limit'], $item['default'], $item['comment'], [
  621. 'options' => $item['values'],
  622. 'default_type' => $item['default_type'],
  623. ]);
  624. }
  625. }
  626. /**
  627. * 创建
  628. * @param array $data
  629. * @return mixed
  630. * @author 等风来
  631. * @email 136327134@qq.com
  632. * @date 2023/4/11
  633. */
  634. public function createCrud(int $id, array $data)
  635. {
  636. $tableName = $data['tableName'];
  637. $tableField = $this->valueReplace($data['tableField']);
  638. $filePath = $this->valueReplace($data['filePath']);
  639. $modelName = !empty($data['modelName']) ? $data['modelName'] : $tableName;
  640. $tableComment = !empty($data['tableComment']) ? $data['tableComment'] : $modelName;
  641. //检测是否为系统表
  642. if (in_array($tableName, self::NOT_CRUD_TABANAME)) {
  643. throw new AdminException('系统数据表,无法生成');
  644. }
  645. $data['softDelete'] = false;
  646. $tableInfo = null;
  647. //先检查表存在则
  648. if ($id) {
  649. $this->updateFromCommon($tableName, $tableComment);
  650. //读取数据库表
  651. $tableInfo = $this->getTableInfo($tableName);
  652. if ($tableInfo) {
  653. //对比字段进行更新/删除字段
  654. $this->diffAlter($tableName, $data['deleteField'], $tableField);
  655. }
  656. } else {
  657. if ($this->dao->count(['table_name' => $tableName])) {
  658. throw new AdminException('表已经被生成过,请在列表中进行修改');
  659. }
  660. }
  661. //创建数据库
  662. $tableCreateInfo = null;
  663. if ($tableField && (!$data['isTable'] || !$tableInfo)) {
  664. $tableCreateInfo = $this->makeDatebase($tableName, $tableComment, $tableField);
  665. if ($tableCreateInfo['softDelete']) {
  666. $data['softDelete'] = true;
  667. }
  668. }
  669. //获取主键
  670. foreach ($tableField as $value) {
  671. if ($value['primaryKey']) {
  672. $data['key'] = $value['field'];
  673. break;
  674. }
  675. }
  676. $routeName = 'crud/' . Str::snake($tableName);
  677. $uniqueAuth = Str::snake($tableName) . '-crud-list-index';
  678. //增加保存的绝对路径
  679. foreach ($filePath as $k => $i) {
  680. if (in_array($k, ['pages', 'router', 'api'])) {
  681. $filePath[$k] = Make::adminTemplatePath() . $i;
  682. } else {
  683. $filePath[$k] = app()->getRootPath() . $i;
  684. }
  685. }
  686. //创建菜单
  687. if (!$data['menuName']) {
  688. $data['menuName'] = $tableName;
  689. }
  690. $dataMenu = [
  691. 'pid' => $data['pid'],
  692. 'menu_name' => $data['menuName'],
  693. 'menu_path' => '/' . $routeName,
  694. 'auth_type' => 1,
  695. 'is_show' => 1,
  696. 'is_show_path' => 1,
  697. 'is_del' => 0,
  698. 'unique_auth' => $uniqueAuth,
  699. 'is_header' => $data['pid'] ? 0 : 1,
  700. ];
  701. $crudInfo = null;
  702. if ($id) {
  703. $crudInfo = $this->dao->get($id);
  704. }
  705. $res = $this->transaction(function () use ($tableComment, $tableCreateInfo, $crudInfo, $modelName, $filePath, $tableName, $routeName, $data, $dataMenu) {
  706. $routeService = app()->make(SystemRouteServices::class);
  707. $meunService = app()->make(SystemMenusServices::class);
  708. //修改菜单名称
  709. if ($crudInfo) {
  710. //菜单存在的时候进行修改
  711. if ($crudInfo->menu_id && $meunService->value(['id' => [$crudInfo->menu_id]], 'id')) {
  712. $meunService->update($crudInfo->menu_id, $dataMenu);
  713. $menuInfo = (object)['id' => $crudInfo->menu_id];
  714. } else {
  715. $menuInfo = $meunService->save($dataMenu);
  716. }
  717. //删除掉添加的路由权限
  718. if ($crudInfo->route_ids) {
  719. $routeService->deleteRoutes($crudInfo->route_ids);
  720. }
  721. //删除掉权限路由
  722. if ($crudInfo->menu_ids) {
  723. app()->make(SystemMenusServices::class)->deleteMenus($crudInfo->menu_ids);
  724. }
  725. } else {
  726. $menuInfo = $meunService->save($dataMenu);
  727. }
  728. //写入路由权限
  729. $cateId = app()->make(SystemRouteServices::class)->topCateId('adminapi', 'CRUD');
  730. $ruleData = [
  731. [
  732. 'path' => $routeName,
  733. 'method' => 'GET',
  734. 'name' => $modelName . '列表接口',
  735. 'app_name' => 'adminapi',
  736. 'cate_id' => $cateId,
  737. 'unique_auth' => '',
  738. 'add_time' => date('Y-m-d H:i:s')
  739. ],
  740. [
  741. 'path' => $routeName . '/create',
  742. 'method' => 'GET',
  743. 'name' => $modelName . '获取创建表单接口',
  744. 'app_name' => 'adminapi',
  745. 'cate_id' => $cateId,
  746. 'unique_auth' => Str::snake($tableName) . '-add',
  747. 'add_time' => date('Y-m-d H:i:s')
  748. ],
  749. [
  750. 'path' => $routeName,
  751. 'method' => 'POST',
  752. 'name' => $modelName . '保存接口',
  753. 'app_name' => 'adminapi',
  754. 'cate_id' => $cateId,
  755. 'unique_auth' => '',
  756. 'add_time' => date('Y-m-d H:i:s')
  757. ],
  758. [
  759. 'path' => $routeName . '/<id>/edit',
  760. 'method' => 'GET',
  761. 'name' => $modelName . '获取修改表单接口',
  762. 'app_name' => 'adminapi',
  763. 'cate_id' => $cateId,
  764. 'unique_auth' => '',
  765. 'add_time' => date('Y-m-d H:i:s')
  766. ],
  767. [
  768. 'path' => $routeName . '/<id>',
  769. 'method' => 'GET',
  770. 'name' => $modelName . '查看数据接口',
  771. 'app_name' => 'adminapi',
  772. 'cate_id' => $cateId,
  773. 'unique_auth' => '',
  774. 'add_time' => date('Y-m-d H:i:s')
  775. ],
  776. [
  777. 'path' => $routeName . '/<id>',
  778. 'method' => 'PUT',
  779. 'name' => $modelName . '修改接口',
  780. 'app_name' => 'adminapi',
  781. 'cate_id' => $cateId,
  782. 'unique_auth' => '',
  783. 'add_time' => date('Y-m-d H:i:s')
  784. ],
  785. [
  786. 'path' => $routeName . '/status/<id>',
  787. 'method' => 'PUT',
  788. 'name' => $modelName . '修改状态接口',
  789. 'app_name' => 'adminapi',
  790. 'cate_id' => $cateId,
  791. 'unique_auth' => '',
  792. 'add_time' => date('Y-m-d H:i:s')
  793. ],
  794. [
  795. 'path' => $routeName . '/<id>',
  796. 'method' => 'DELETE',
  797. 'name' => $modelName . '删除接口',
  798. 'app_name' => 'adminapi',
  799. 'cate_id' => $cateId,
  800. 'unique_auth' => '',
  801. 'add_time' => date('Y-m-d H:i:s')
  802. ],
  803. ];
  804. $routeList = $routeService->saveAll($ruleData);
  805. $routeIds = array_column($routeList->toArray(), 'id');
  806. //记录权限加入菜单表
  807. $menuData = [];
  808. foreach ($ruleData as $item) {
  809. $menuData[] = [
  810. 'pid' => $menuInfo->id ?: 0,
  811. 'methods' => $item['method'],
  812. 'api_url' => $item['path'],
  813. 'unique_auth' => $item['unique_auth'],
  814. 'menu_name' => $item['name'],
  815. 'is_del' => 0,
  816. 'auth_type' => 2,
  817. ];
  818. }
  819. $menus = app()->make(SystemMenusServices::class)->saveAll($menuData);
  820. $menuIds = array_column($menus->toArray(), 'id');
  821. //生成文件
  822. $make = $this->makeFile($tableName, $routeName, true, $data, $filePath);
  823. $makePath = [];
  824. foreach ($make as $key => $item) {
  825. $makePath[$key] = $item['path'];
  826. }
  827. if ($tableCreateInfo && isset($tableCreateInfo['table']) && $tableCreateInfo['table'] instanceof Table) {
  828. //创建数据库
  829. $tableCreateInfo['table']->create();
  830. }
  831. $crudDate = [
  832. 'pid' => $data['pid'],
  833. 'name' => $data['menuName'],
  834. 'model_name' => $data['modelName'],
  835. 'table_name' => $tableName,
  836. 'table_comment' => $tableComment,
  837. 'table_collation' => self::TABLR_COLLATION,
  838. 'field' => json_encode($data),//提交的数据
  839. 'menu_ids' => json_encode($menuIds),//生成的菜单id
  840. 'menu_id' => $menuInfo->id,//生成的菜单id
  841. 'make_path' => json_encode($makePath),
  842. 'route_ids' => json_encode($routeIds),
  843. ];
  844. if ($crudInfo) {
  845. $res = $this->dao->update($crudInfo->id, $crudDate);
  846. } else {
  847. $crudDate['add_time'] = time();
  848. //记录crud生成
  849. $res = $this->dao->save($crudDate);
  850. }
  851. return $res;
  852. });
  853. return $res->toArray();
  854. }
  855. /**
  856. * 获取数据库配置
  857. * @return array
  858. */
  859. protected function getDbConfig(): array
  860. {
  861. $default = app()->config->get('database.default');
  862. $config = app()->config->get("database.connections.{$default}");
  863. if (0 == $config['deploy']) {
  864. $dbConfig = [
  865. 'adapter' => $config['type'],
  866. 'host' => $config['hostname'],
  867. 'name' => $config['database'],
  868. 'user' => $config['username'],
  869. 'pass' => $config['password'],
  870. 'port' => $config['hostport'],
  871. 'charset' => $config['charset'],
  872. 'table_prefix' => $config['prefix'],
  873. ];
  874. } else {
  875. $dbConfig = [
  876. 'adapter' => explode(',', $config['type'])[0],
  877. 'host' => explode(',', $config['hostname'])[0],
  878. 'name' => explode(',', $config['database'])[0],
  879. 'user' => explode(',', $config['username'])[0],
  880. 'pass' => explode(',', $config['password'])[0],
  881. 'port' => explode(',', $config['hostport'])[0],
  882. 'charset' => explode(',', $config['charset'])[0],
  883. 'table_prefix' => explode(',', $config['prefix'])[0],
  884. ];
  885. }
  886. $table = app()->config->get('database.migration_table', 'migrations');
  887. $dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $table;
  888. return $dbConfig;
  889. }
  890. public function getAdapter()
  891. {
  892. $options = $this->getDbConfig();
  893. $adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options);
  894. if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
  895. $adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
  896. }
  897. return $adapter;
  898. }
  899. /**
  900. * 创建数据库
  901. * @param string $tableName
  902. * @param string $tableComment
  903. * @param array $tableField
  904. * @return array
  905. * @author 等风来
  906. * @email 136327134@qq.com
  907. * @date 2023/4/7
  908. */
  909. public function makeDatebase(string $tableName, string $tableComment, array $tableField = [], string $collation = self::TABLR_COLLATION)
  910. {
  911. $timestampsField = [];
  912. $softDelete = false;
  913. $timestamps = false;
  914. $indexField = [];
  915. //创建表
  916. $table = new Table($tableName, ['comment' => $tableComment, 'collation' => $collation], $this->getAdapter());
  917. //创建字段
  918. foreach ($tableField as $item) {
  919. if (isset($item['primaryKey']) && $item['primaryKey']) {
  920. continue;
  921. }
  922. $option = [];
  923. if (isset($item['limit'])) {
  924. $option['limit'] = (int)$item['limit'];
  925. }
  926. if (isset($item['default']) && isset($item['default_type'])) {
  927. switch ($item['default_type']) {
  928. case '1'://自定义
  929. $option['default'] = $item['default'];
  930. break;
  931. case '2'://为null
  932. $option['null'] = true;
  933. break;
  934. case '3'://时间
  935. $option['default'] = Db::raw('CURRENT_TIMESTAMP');
  936. break;
  937. }
  938. }
  939. //创建伪删除
  940. if ($item['field_type'] === 'addSoftDelete') {
  941. $table->addSoftDelete();
  942. $softDelete = true;
  943. } else if ($item['field_type'] == 'timestamp' &&
  944. ($item['field'] === 'create_time' || $item['field'] === 'update_time')) {
  945. $timestampsField[] = $item;
  946. } else {
  947. $option['comment'] = $item['comment'];
  948. $fieldType = $this->changeTabelRule($item['field_type']);
  949. if (in_array($fieldType, ['text', 'longtext', 'tinytext'])) {
  950. unset($option['limit']);
  951. }
  952. //判断字段类型
  953. if ($fieldType == 'boolean' && isset($option['default']) && $option['default'] === '') {
  954. unset($option['default']);
  955. }
  956. //兼容枚举字段
  957. if ($fieldType == 'enum') {
  958. unset($option['limit']);
  959. $option['values'] = $item['limit'];
  960. }
  961. $table->addColumn($item['field'], $this->changeTabelRule($item['field_type']), $option);
  962. }
  963. }
  964. //创建索引
  965. if (!empty($data['tableIndex'])) {
  966. $indexField = $data['tableIndex'];
  967. foreach ($data['tableIndex'] as $item) {
  968. $table->addIndex($item);
  969. }
  970. }
  971. //如果是成对出现的create_time和update_time就直接增加修改和添加时间
  972. if (count($timestampsField) == 2) {
  973. //创建修改和增加时间
  974. $table->addTimestamps();
  975. $timestamps = true;
  976. } else {
  977. //如果是一个数组,增加一列
  978. foreach ($timestampsField as $item) {
  979. $option['comment'] = $item['comment'];
  980. $table->addColumn($item['field'], $this->changeTabelRule($item['field_type']), $option);
  981. }
  982. }
  983. return compact('indexField', 'softDelete', 'timestamps', 'table');
  984. }
  985. /**
  986. * 创建文件返回文件路径和内容
  987. * @param string $tableName
  988. * @param string $routeName
  989. * @param bool $isMake
  990. * @param array $options
  991. * @param array $filePath
  992. * @param string $basePath
  993. * @return array[]
  994. * @author 等风来
  995. * @email 136327134@qq.com
  996. * @date 2023/4/7
  997. */
  998. public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = [], string $basePath = '')
  999. {
  1000. $options['fromField'] = is_array($options['fromField']) ? $options['fromField'] : [];
  1001. $options['columnField'] = is_array($options['columnField']) ? $options['columnField'] : [];
  1002. //生成模型
  1003. $model = app()->make(Model::class);
  1004. $model->setFilePathName($filePath['model'] ?? '')->setbasePath($basePath)->handle($tableName, $options);
  1005. //生成dao
  1006. $dao = app()->make(Dao::class);
  1007. $dao->setFilePathName($filePath['dao'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1008. 'usePath' => $model->getUsePath(),
  1009. 'modelName' => $options['modelName'] ?? '',
  1010. 'searchField' => $options['searchField'] ?? [],
  1011. ]);
  1012. //生成service
  1013. $service = app()->make(Service::class);
  1014. $service->setFilePathName($filePath['service'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1015. 'field' => $options['fromField'],
  1016. 'columnField' => $options['columnField'],
  1017. 'key' => $options['key'],
  1018. 'usePath' => $dao->getUsePath(),
  1019. 'modelName' => $options['modelName'] ?? '',
  1020. 'hasOneField' => $options['hasOneField'] ?? [],
  1021. ]);
  1022. //生成验证器
  1023. $validate = app()->make(Validate::class);
  1024. $validate->setFilePathName($filePath['validate'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1025. 'field' => $options['fromField'],
  1026. 'modelName' => $options['modelName'] ?? '',
  1027. ]);
  1028. //生成控制器
  1029. $controller = app()->make(Controller::class);
  1030. $controller->setFilePathName($filePath['controller'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1031. 'usePath' => $service->getUsePath(),
  1032. 'modelName' => $options['modelName'] ?? '',
  1033. 'searchField' => $options['searchField'] ?? [],
  1034. 'columnField' => $options['columnField'] ?? [],
  1035. 'validateName' => '\\' . str_replace('/', '\\', $validate->getUsePath()) . 'Validate::class',
  1036. 'field' => array_column($options['fromField'], 'field'),
  1037. ]);
  1038. //生成路由
  1039. $route = app()->make(Route::class);
  1040. $route->setFilePathName($filePath['route'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1041. 'menus' => $options['modelName'] ?? $options['menuName'],
  1042. 'route' => $routeName
  1043. ]);
  1044. //生成前台路由
  1045. $viewRouter = app()->make(ViewRouter::class);
  1046. $viewRouter->setFilePathName($filePath['router'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1047. 'route' => $routeName,
  1048. 'menuName' => $options['menuName'],
  1049. 'modelName' => $options['modelName'] ?? $options['menuName'],
  1050. ]);
  1051. //生成前台接口
  1052. $viewApi = app()->make(ViewApi::class);
  1053. $viewApi->setFilePathName($filePath['api'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1054. 'route' => $routeName,
  1055. ]);
  1056. //生成前台页面
  1057. $viewPages = app()->make(ViewPages::class);
  1058. $viewPages->setFilePathName($filePath['pages'] ?? '')->setbasePath($basePath)->handle($tableName, [
  1059. 'field' => $options['columnField'],
  1060. 'tableFields' => $options['tableField'] ?? [],
  1061. 'searchField' => $options['searchField'] ?? [],
  1062. 'route' => $routeName,
  1063. 'key' => $options['key'],
  1064. 'pathApiJs' => '@/' . str_replace('\\', '/', str_replace([Make::adminTemplatePath(), '.js'], '', $viewApi->getPath())),
  1065. ]);
  1066. //创建文件
  1067. if ($isMake) {
  1068. FileService::batchMakeFiles([$model, $validate, $dao, $service, $controller, $route, $viewApi, $viewPages, $viewRouter]);
  1069. }
  1070. return [
  1071. 'controller' => [
  1072. 'path' => $this->replace($controller->getPath()),
  1073. 'content' => $controller->getContent()
  1074. ],
  1075. 'model' => [
  1076. 'path' => $this->replace($model->getPath()),
  1077. 'content' => $model->getContent()
  1078. ],
  1079. 'dao' => [
  1080. 'path' => $this->replace($dao->getPath()),
  1081. 'content' => $dao->getContent()
  1082. ],
  1083. 'route' => [
  1084. 'path' => $this->replace($route->getPath()),
  1085. 'content' => $route->getContent()
  1086. ],
  1087. 'service' => [
  1088. 'path' => $this->replace($service->getPath()),
  1089. 'content' => $service->getContent()
  1090. ],
  1091. 'validate' => [
  1092. 'path' => $this->replace($validate->getPath()),
  1093. 'content' => $validate->getContent()
  1094. ],
  1095. 'router' => [
  1096. 'path' => $this->replace($viewRouter->getPath()),
  1097. 'content' => $viewRouter->getContent()
  1098. ],
  1099. 'api' => [
  1100. 'path' => $this->replace($viewApi->getPath()),
  1101. 'content' => $viewApi->getContent()
  1102. ],
  1103. 'pages' => [
  1104. 'path' => $this->replace($viewPages->getPath()),
  1105. 'content' => $viewPages->getContent()
  1106. ],
  1107. ];
  1108. }
  1109. protected function replace(string $path)
  1110. {
  1111. return str_replace([app()->getRootPath(), Make::adminTemplatePath()], '', $path);
  1112. }
  1113. /**
  1114. * @param string $tableName
  1115. * @param bool $fullName
  1116. * @return string
  1117. * @author 等风来
  1118. * @email 136327134@qq.com
  1119. * @date 2023/4/7
  1120. */
  1121. public function getTableName(string $tableName, bool $fullName = true)
  1122. {
  1123. $tablePrefix = config('database.connections.mysql.prefix');
  1124. $pattern = '/^' . $tablePrefix . '/i';
  1125. return ($fullName ? $tablePrefix : '') . (preg_replace($pattern, '', $tableName));
  1126. }
  1127. }