如何设置数据库视图夹具?

问题描述 投票:0回答:1

是否可以设置一个固定装置来创建数据库视图而不是 CakePHP 中的数据库表?在创建表的夹具和另一个应该是数据库视图的夹具中拥有相同的数据似乎效率低下。

php cakephp
1个回答
1
投票

我设法这样做,其中 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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.