与 sfDoctrineGuard 用户表 (symfony) 的两个表关系 [英] Two tables relation with sfDoctrineGuard user table (symfony)

查看:53
本文介绍了与 sfDoctrineGuard 用户表 (symfony) 的两个表关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的 Web 应用程序中启动了一个用户需要进行身份验证才能使用它的部分.我有两个相关的表:客户和企业......第一个是想要购买产品的用户,第二个是想要销售产品的用户".

I have started in my web application a part who users needs to be autenticated to work with it. I have two tables related: Customer and Enterprise.... the first one are users who want to buy a product and the second one are "users" who want to sell products.

有什么更好的方法来做到这一点?与 user_table 的关系是 1:1 吗?我如何区分哪一种用户类型?因为用户类型只能编辑一些信息,而企业可以访问其他模块...

What is better way to do that? Relation 1:1 with user_table? how can i differentiate wich one user type is? Because user types only can edit some information and enterprise have acces to another modules...

非常感谢.

推荐答案

您可以使用 hasReference() 模型上的函数.

You could use the hasReference() function on the model.

例如您的模型 CustomerUser 有如下关系:

E.g. your model Customer has a relation to User like this:

Customer:
  relations:
    user:
      local: id
      foreignID: id
      foreignAlias: customer

然后你可以测试用户是否是customer类型(在控制器中):

Then you can test whether the user is of type customer (in the controller):

$this->getUser()->getGuardUser()->hasReference('customer');

为了使这更容易,您可以将此方法添加到您的 myUser 类:

To make this easier you can add this method to your myUser class:

public function isCustomer() {
    return $this->getGuardUser()->hasReference('customer');
}

企业当然也一样.

即使使用两个不同的表,您也可以使用 hasCredential() 方法,如果您只想检查权限,这是最简单的方法.
如果您想访问企业和客户用户的某些属性,您也可以将这两种方法结合起来.

Even using two different tables, you can make use of the hasCredential() method, and this is the easiest way if you only want to check for permission.
If you want to access certain attributes of the enterprise and customer user you can also combine both approaches.

更新:

好吧,假设客户和企业用户具有不同的权限,我会采用用户组方法.这更适合 sfGuard 的组模型.如果您随后知道某个用户在 Customer 组中,那么您就知道该用户具有对客户对象的引用.

Well, assuming that Customers and Enterprise users have different permissions, I would go with the user group approach. This fits better to the group model of sfGuard. If you then know that a user is in group Customer, you know that the user has a reference to a customer object.

但这当然意味着您必须将正确的组分配给新用户才能正常工作.

But this means of course that you have to assign the the right group to a new user in order to work correctly.

这篇关于与 sfDoctrineGuard 用户表 (symfony) 的两个表关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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