Two tables relation with sfDoctrineGuard user table (symfony)

时光毁灭记忆、已成空白 提交于 2019-12-25 09:41:08

问题


Hy,

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.

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...

Thanks a lot.


回答1:


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

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

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

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

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

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

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

Same of course for Enterprise.

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.

Update:

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.




回答2:


Do your tables really need to be separate? If not, you could use the standard sf_guard_user table provided with the sfGuard plugin for both types of user, and then use the groups functionality to assign the user to either the "Customer" group or the "Enterprise" group.

Checking the group that the user belonged to would allow you to display (or hide) the appropriate content for that user type. Something like this in your action:

if ($this->getUser()->hasCredential("enterprise"))
{
  // code related to Enterprise customers only
}

or using the $sf_user variable in your view/templates.



来源:https://stackoverflow.com/questions/2183552/two-tables-relation-with-sfdoctrineguard-user-table-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!