cakephp – 如何设置数据库视图夹具?
发布时间:2020-12-13 15:59:39 所属栏目:PHP教程 来源:网络整理
导读:是否可以在Cake PHP中设置一个可以创建数据库视图而不是数据库表的工具?在创建表的夹具中以及应该是数据库视图的另一个夹具中使用相同的数据似乎是低效的. 解决方法 我设法这样做,其中view_my_model.sql包含生成数据库视图的SQL: class ViewMyModelFixture
是否可以在Cake
PHP中设置一个可以创建数据库视图而不是数据库表的工具?在创建表的夹具中以及应该是数据库视图的另一个夹具中使用相同的数据似乎是低效的.
解决方法
我设法这样做,其中view_my_model.sql包含生成数据库视图的SQL:
class ViewMyModelFixture extends AppFixture { public function create($DboSource) { $path = APP . 'Config' . DS . 'sql' . DS . 'view_my_model.sql'; $File = new File($path); if (!$File->exists()) { throw new CakeException($path . ' does not exist'); } if (!$File->readable()) { throw new CakeException($path . ' is not readable'); } $sql = $File->read(); if (empty($sql)) { throw new CakeException($path . ' has no SQL'); } try { $DboSource->execute($sql); $this->created[] = $DboSource->configKeyName; } catch (Exception $exception) { $msg = __d('cake_dev','Fixture creation for "%s" failed "%s"',$this->table,$exception->getMessage()); CakeLog::error($msg); trigger_error($msg,E_USER_WARNING); return false; } return true; } public function drop($DboSource) { try { $sql = 'DROP VIEW ' . $DboSource->fullTableName($this->table) . ";"; $DboSource->execute($sql); $this->created = array_diff($this->created,array($DboSource->configKeyName)); } catch (Exception $exception) { return false; } return true; } public function truncate($DboSource) { return; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |