Yii2:如何让ActiveRecord模型只看到某些数据库的数据? [英] Yii2: How to let ActiveRecord models only see certain database data?

查看:126
本文介绍了Yii2:如何让ActiveRecord模型只看到某些数据库的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个多租户应用程序。我正在使用相同的code不同的客户。但每个客户都有自己的入口脚本有自己的配置。客户ID是可访问的的Yii :: $ APP-> PARAMS ['客户ID'] 。注:我必须区分客户,那是因为没有登录/验证。总之,假设控制器动作被调用,客户ID给出的参数。

I'm trying to implement a multi-tenant application. I have different customers that are using the same code. But each customer gets its own entry script with its own configuration. The customer id is accessible by Yii::$app->params['customerId']. Note: I have to distinguish the customers that way because there is no login/authentication. Anyway, assume the controller action is called and a customer id is given with the parameter.

现在我有一些表,每个人都有为客户ID列。我已经创建模型类与GII所有的罚款。但现在我已经得到了我要检查或告诉为其客户ID已被使用的每个数据库操作的问题。因此,对每一个数据库操作(查询),我可以添加一个 - > andWhere(['客户ID =>的Yii :: $ APP-> PARAMS ['客户ID'])。这是繁琐且容易出错,当然。

Now I have some tables and each has a column for the customer id. I have created model classes with gii and all is fine. But now I've got the problem that I have to check or tell for each database operation which customer id has to be used. So on every db operation (Query) I can add an ->andWhere(['customerId' => Yii::$app->params['customerId']]). This is tedious and error prone, of course.

有没有办法简化这​​个?想想看,有查找,插入,更新,删除和连接操作。我大概可以使用定制查询类,但我不知道我要在那里做,我不知道这是否有助于插入/更新/删除。想想也是,有与同一客户ID科拉姆相关的表。 的hasMany() hasOne()也应该反映出这一点。

Is there a way to simplify this? Consider that there are find, insert, update, delete and join operations. I can probably use Customized query classes but I don't know what I have to do there and I'm not sure if it helps with inserts/updates/deletes. Consider also that there are related tables with the same customer id colum. hasMany() and hasOne() should also reflect this.

我的目标是让客户ID是尽可能透明。我不想去想它。所以,我能想象某种抽象查询类,使用某列(客户ID列)的所有操作和获取客户ID以某种方式(注射或的Yii :: $ APP-> PARAMS ['客户ID'] )。

My aim is to have the customer id be as transparent as possible. I don't want to think about it. So I could imagine some kind of abstract query class that uses a certain column (the customer id column) for all operations and that gets the customer id somehow (injected or with Yii::$app->params['customerId']).

我的假设是,我需要做到这一点主要体现在模型中。我想,控制器通过这个话题不太关注。

My assumption is that I need to do this mainly in the model. I think controllers are less concerned by this topic.

任何建议如何做到这一点?

Any suggestions how to do that?

推荐答案

在像你描述的形式解决方案,它是用能过滤数据的客户ID数据库视图管理数据的那些情况。这包括动态获取表/视图,您可以通过模型的改变功能tableName值()管理。创建视图也可与功能DML数据库自动化。这只是一个简单的介绍我希望这对您有用。

In situations like the ones you described a form solution it is to manage data with database views able to filter the data for customer id. This involves dynamic access to the tables / views that you can manage through changing function tableName () of the model. Creating views can also be automated with functions DML database. This is only a brief introduction i hope this is useful for you.

这篇关于Yii2:如何让ActiveRecord模型只看到某些数据库的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆