Two tables relation with sfDoctrineGuard user tabl

2019-09-03 07:21发布

问题:

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.