- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 CakePHP 的新手,使用的是 1.3 版。
如何在任何数据库操作之前动态更改 DATABASE_CONFIG 中的“模式”属性?我可以在任何数据库交互之前执行特定于 postgres 的命令“set search_path to 'schema_xyz'”的类是什么?
我想利用 Postgres 的能力在单个数据库中维护多个不同的命名空间(在 Postgres 的说法中也称为模式),以在我的应用程序中实现 Multi-Tenancy 。也就是说,每个命名空间都将包含相同的一组表,但显然内容不同。在这里,重要的是不要将模式理解为意思 table metadata ,而是作为 postgres-specific concept of namespace其中模式是表的容器。确切的 Postgres 命令并不重要。什么是它可以被调用的机制,并且避开了 Cake 表描述的典型含义,如 SchemaShell 中所见。我发现 Cake 暴露 namespace 概念的唯一地方是在 database.php 文件中,然后在首次建立数据库连接时使用该文件。参见:api13.cakephp.org/view_source/dbo-postgres/#line-113(新用户链接限制,抱歉)
if ($this->connection) {
$this->connected = true;
$this->_execute("SET search_path TO " . $config['schema']);
我想在所有数据库查询之前设置 search_path,而不是像目前所做的那样仅在连接时设置。
作为概念证明,我尝试在我的模型中设置 $useDbConfig,但根据打印 SQL 命令的调试输出,这似乎只影响所有查询的一个子集。我已将其移至 app_model.php 中,结果相同。通过动态创建 db_config 实例并通过 loadDataSource 传递给 ConnectionManager 来增强它。也许我应该用各种风格的 before... 方法来编写代码。
我在网上看到很多帖子,人们讨论使用 database.php 中的几种数据库配置之一来为开发、实验室和生产环境使用不同的数据库。但是我有一个包含多个 namespace /模式的数据库。此外,我的此类 namespace 的数量太多且动态变化,无法在 database.php 中对新变量进行硬编码。
因此,在 CakePHP 的哪个位置我可以插入一些东西来在任何数据库命令之前设置 search_path?我稍后会处理优化。请记住,我是 Cake 的新手,所以请尽量具体。如果我可以澄清这个问题,请告诉我。
提前致谢。这是部分工作的代码片段:
class AppModel extends Model {
function beforeFind($queryData)
{
App::import("ConnectionManager");
$cm = &ConnectionManager::getInstance();
$namespace = 'xyz_namespace'; //name of the new schema/namespace/search path
$new_db_config_name = 'new_config'; //name for the new DB config to be used in the ConnectionManager
$new_db_config = $cm->config->default; //copy the 'default' DB config into an array
$new_db_config['schema'] = $namespace; //change/add the new schema/namespace/search path
$cm->create($new_db_config_name, $new_db_config); //turn the array into a DbConfig object
$cm->loadDataSource($new_db_config_name); //load the new DbConfig into the ConnectionManager
$this->useDbConfig = $new_db_config_name; //tell the model to new use the Db Config
return $queryData;
}
}
最佳答案
如果你想为每个登录角色切换模式,在 PostgreSQL 中有一个非常简单的方法:
ALTER ROLE foo SET search_path=bar, public;
ALTER ROLE baz SET search_path=bam, public;
因此,由该角色发起的连接会自动设置 search_path。
如果您的登录名与所需的模式名称相同,还有一种更简单的方法,我引用 the fine manual :
If one of the list items is the special value $user, then the schema having the name returned by SESSION_USER is substituted, if there is such a schema. (If not, $user is ignored.)
但请注意 - the fine manual再次:
Role-specific variable settings take effect only at login; SET ROLE and SET SESSION AUTHORIZATION do not process role-specific variable settings.
关于postgresql - 在 CakePHP 中使用特定命令在任何数据库访问之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7758746/
经验丰富的程序员,但对 CakePHP 2.1 不熟悉,我花了一天的时间努力让自定义 Helper 在 View 中工作,遵循手册:http://book.cakephp.org/2.0/en/vie
任何人都可以列出 CakePHP 框架相对于其他 PHP 框架的缺点吗? 1) 请解释为什么缺少的功能对 PHP 项目如此重要? 2) 该缺失的功能如何帮助那些使用具有该功能的框架的 Web 开发人员
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我对 PHP 比较陌生。开始学习 PHP,但后来遇到了 cakePHP,它可以加快开发时间。 阅读文档和博客教程后,我仍然不明白命名约定。我想在我开始做一些例子之前我不会知道,但是为了让我开始,有人可
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
任何人都可以告诉我为什么当一个字段没有翻译时它返回空,而不是默认翻译? 我会感谢所有建议... 不知道你想看我代码的哪一部分,因为它都是直接来自 www.book.cake.org。所以我粘贴了一些代
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在编写一个支持多种测量单位的应用程序。在极少数情况下,用户想要更改他们的测量系统,我需要运行一个查询,该查询应用乘数将应用程序中的每个单位列缩放到正确的测量系统。为了确保所有数据在此操作出现问题时
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 8年前关闭
在用户模型中: var $hasMany = array( 'Photo' => array( 'className' => 'Photo',
剧透:我想更好地了解对 CakePHP 应用程序的请求的生命周期。 背景:我使用的是 CakePHP v2.3。我正在调试一个问题,该问题导致我无法找到的不想要的重定向。我正在尝试通过增量添加 die
在我的 Cake 应用程序中,我有一个 Controller “completed_projects”。它的索引操作不带参数并列出了一些项目。可以访问不同的页面example.com/complete
我找不到有关cakeError()定义为成员函数的任何信息。 documentation仅声明该调用看起来像这样: $this->cakeError(string $errorType [, arra
我创建了一个自定义数据源,该数据源从Web api获取数据,现在我正在研究实现错误处理。 在数据源中,我正在调用$ model-> onError()。在模型中,我创建了onError方法,并且可以使
除了重定向和渲染之外,还有其他命令可以将 Controller 重定向到特定 View 页面吗? 我的重定向为 $this->redirect('/forms/homepage/'.$userId);
我试图在我的字段中查找计数并连接该计数,但它给出错误消息,因为找不到 make_count 并且我在上面创建了相同的字段。 $this->Car->virtualFields['make_count'
我想要条件验证,即在更新密码时隐藏字段,并且密码验证为“6 到 15”个字符!因此密码以哈希格式存储在数据库中,因此不允许我更新。我的模型代码如下, array( 'rule
echo $form->input( 'country_id', array( 'type' => 'select', 'label' => __('C
我正在使用 CakePHP 创建一个锦标赛平台。目前,我已经创建了以下可以完美运行的表格、模型和 Controller :锦标赛、用户和团队。 我还创建了一个“Tournamentuser”表、 Co
我的场景是这样的: 在我的/views/layout/default.ctp 在我的/views/pages/home.ctp Html-
我是一名优秀的程序员,十分优秀!