使用 Doctrine 在 Symfony 多租户应用程序中进行数据分离
我正在尝试实现一个多租户应用程序,即 - 所有客户的数据都存储在一个数据库中 - 每个共享表都有一个tenant_id
字段来分隔数据
我希望通过添加where('tenant_id = ', $user->getTenantID())
{伪代码} 所有 SELECT 查询 我无法预先找到任何解决方案,但以下是我正在考虑的可能方法。 1)粗略的方法: 自定义每个类中的所有 fetchAll
和 fetchOne
函数(我会发疯的!) 2)使用监听器: 可能为 preDqlSelect
事件进行编码并将“where”添加到所有查询 3) 覆盖 buildQuery
(): 找不到前端的示例 4)实现contentformfilter
:再次需要一个指针,
如果有人可以验证这些&,将不胜感激。对效率、适用性进行评论。 另外,如果有人使用其他策略实现了多租户,请分享。谢谢
I am trying to implement a multi-tenant application, that is
- data of all clients in a single database
- each shared table has a tenant_id
field to separate data
I wish to achieve data separation by adding where('tenant_id = ', $user->getTenantID())
{pseudoc-code}
to all SELECT queries
I could not find any solution up-front, but here are possible approaches I am considering.
1) crude approach:
customizing all fetchAll
and fetchOne
functions in every class (I will go mad!)
2) using listeners:
possibly coding for the preDqlSelect
event and adding the 'where' to all queries
3) override buildQuery
(): could not find an example of this for front-end
4) implement contentformfilter
: again need a pointer
Would appreciate if someone could validate these & comment on efficieny, suitability.
Also, if anyone has achieved multitenancy using another strategy, pl share. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在通过编写 preDqlSelect 事件来使用 Doctrine Record Listeners 制定解决方案。我认为这是最好的&以通用方式执行操作的最简单方法,而不必修改每个表类并编写租户感知查询。有了监听器,多租户将对开发人员完全透明。
感谢您的参与。
I'm working out a solution using Doctrine Record Listeners by coding the preDqlSelect event. I think this is the best & easiest way to do things in a generic way, rather than having to modify every Table class and writing Tenant aware queries. With listeners, multi-tenancy will be completely transparent to developers.
Thanks for participating.
我相信更可行和安全的方法是创建一个新函数来检索租户感知查询,这是一个示例...将 MyModel 替换为表的名称:
然后要使用这个新函数,只需调用:
以这种方式您在需要时创建“租户感知查询”...您只需在需要时使用此功能...即使在管理生成器中,配置文件中也有一种方法可以覆盖默认查询方法:
唯一剩下的就是创建这个方法。
希望这个答案对你有用 =)
I belive that the more feasible and secure way is to create a new function to retrieve a tenant aware query, this is an example... Replace MyModel with the name of your table:
Then to consume this new function just call:
In this way you create a "Tenant Aware query" when needed... you just use this function when needed... Even in the admin generator there is a way in the configuration file to override the default query method:
The only thing left is to create this method.
Hope this answer works for you =)
我已经发布了 sfMultiTenantPlugin,在这里找到它:
http://www.symfony-project.org/plugins/sfMultiTenantPlugin
I have published the sfMultiTenantPlugin, find it here:
http://www.symfony-project.org/plugins/sfMultiTenantPlugin