MySQL User with Roles, Role specific fields? - php

I have an application where a User can be a Supplier or a Shop.
Now these 2 roles have very different role-specific fields..
Basically, a Shop can make Orders to Suppliers.. So a Shop can make Orders and a Supplier will receive those Orders.
Where would I put these fields for good practice?
Add them all on the User class, with nullable types (doesn't seem right!)
Create a different class for each Role. So we have a Supplier class with a user_id and Shop class with a user_id, I would have to query these fields using $user->supplier->field or $user->shop->field...
Neither of these seems like thé way to go. Any idea's?
Thank you!

You don't have to stick with a single users table. You can have a Supplier model and a Shop model (with suppliers and shops tables).
Then in the config/auth.php file, you can set up a new auth provider using the Eloquent driver but with a different model. You can then assign those providers to any new guards that you create.
You can read more about authentication and authorisation here:
https://laravel.com/docs/5.4/authentication
https://laravel.com/docs/5.4/authorization
Edit
After it was mentioned in the comments about polymorphic relations, I think that would apply better for this type of relationship (multiple types of users, each with own set of fields).
Read more here:
https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations

Can a user have multiple shops or supply multiples? Your second option seems fine.
User hasMany: supplier, shop
- id
- name
Supplier belongsTo, hasOne: user
- id
- user_id
- fields_supplier
Shop belongsTo, hasOne: user
- id
- user_id
- fields_shop
$user->supplier->fields_supplier;
$user->shop->fields_shop;
$supplier->user->name;
$shop->user->name;
--
If you had more info on how you wanted it to work, it would help.

"Supplier" is a role played by a "Party" ("Individual" or "Organization")
Your store may get a Catalog from a Supplier, and may Request Quotes from them.
Your store creates "Purchase Orders" and sends them to the Supplier, and they send you "Order Responses"
A Supplier is not a user. You could create "User"s for their employees though
Why not just use an existing ERP like Odoo?

Related

Employees for Merchant in E-commrece?

I have an e-commerce project that contains there type of user admins,merchants and normall user in merchant section I need to make employees for merchant, like each merchant will have many employees with him and I'm confused between two structures for database first one:
In the same merchants table add another column named ( merchant_id ) it belongs to the parent of merchant.
id - name - email - password - merchant_id
The second:
Make a separate table like ( employees ) that the merchant has many employees.
id- name- email - password - merchant_id
The problem that I'm confused with is like when I give the employees permissions!
please guy's any suggestions for something like this probelm ?
I created a diagram to show my point of view. Please consider the following image:
Explaining the diagram:
A merchand hasMany Employees;
An employee belongsTo a merchand (foreign key merchand_id to tbl_merchands);
An employee belongsTo a permission (foreign key permission_id to tbl_permissions);
A permissions hasMany employees;
Code the relations in the models and you are good to go. To validate if a user has permission to access some Route(), you can use a middleware.
NOTE: I created a tbl_permissions because of your request. Maybe I would rename to tbl_groups (admins, users, etc) and use the table globally.
Hope it guides you. Regards!
COMMENT #1:
To control the page access you may use a middleware. You can create a table for products with foreign keys to merchand_id and employee_id. To get products associated to an employee, you need to create a Model plus the relation to the products. To retrive the data, do Employees::find($employee_id)->products. Hope you know how to code in laravel otherwise you will have an hard time coding this tips.

PHP doctrine2, how to create a user with multiple roles

I am in the process of switching to the Doctrine ORM. In our application we have 2 roles a user can take on: customer and employee.
Previously we had a user's table, then a customer table and an employee table.
A user can be a customer, or an employee. A user can have multiple instances of employee. (They work for more than one company at the same time.) However, that user can also be a customer. So they should have an entry in the customer table as well.
Previously we had a relation like this:
users
- id
company_has_employee
- company_id
- employee_id
employees
- user_id
- other attributes
customers
- user_id
- other attributes
So as you can see there is only one entry in the user table (meaning the user only has one set of credentials for all of their roles.)
How can I translate this functionality into doctrine?
I have experimented with Class Table Inheritance in the Doctrine Docs but I feel as if this is more complicated than that.

Laravel 4 add User and Child

I am writing a web app where there are users with different account types and roles. The DB is set up in such a way where there is a Users table and then a Customers table which carries the related user_id.
I have set up the relationship with the Users Eloquent model and the Customers Eloquent model so the fetching of records works properly.
I was wondering however if there is a way within Laravel to apply the same logic for inserting or updating. For example, I am adding a new customer to the db. This requires adding certain data to the Users table and then certain data to the Customers table. Is there a smart way to do this in Laravel instead of adding the user data and then making sure the data was added before adding the customer as the relationship is already defined?
Thanks!

Create user roles and profiles in Codeigniter using Ion Auth

I'm using Codeigniter with Ion Auth for user management. I'd like to create two different user roles - one for volunteers and one for organizations. I know that I can use Ion Auth's groups for things like access control, but what would be a good way to add fields to the different user types (for instance - the volunteer user will have a 'languages spoken' field while the organization will have a 'mission' field)? Should I extend the Ion Auth class for each new user type and handle CRUD seperately for each type, or use the 'groups' field and the user id to reference the fields in another table? Any insight as to an approach to this common problem?
I would recommend just adding all the fields you need into the meta table and only updating the ones you need per user group. If you need multiple groups per user check out Ion Auth v2.
I had the same problem before, what I ended up doing was building relation tables to handle different groups with different fields. Then I modified the profile() code a bit, to join the additional relation table according to the user's group settings.
I would start off building a relational database.
example:
**volunteers table**
id
user_id
languages
**organizations table**
id
user_id
mission
Then depending on user group, join the table in profile() function.
Hope this helps!

Symfony Doctrine how to sort by admin generator virtual columns

http://www.symfony-project.org/jobeet/1_4/Doctrine/en/12
The section "Virtual" columns details how to add columns to lists that belong to related models.
For example, I have 2 model classes: Company and User. A User is always a member of a Company.
For the User list I want to show the Company name, I can do this by adding a getter to the User class getCompanyName() and then in my generator.yml for the User module I would use the field name company_name.
This works and displays the Company name for each User, but how can I sort the list by using the Company name?
I solve this by adding a table_method, then when I implement the table method in the model, I innerjoin Company and order by a field in company. Not sure if this can be extended to sorting columns in the generated screens, but I think this works as well.

Categories