Deleting associated Data in cakephp - cakephp

I have two Models, departments and users. Departments is associated by hasMany with users and users belongsTo a department. Below is the snapshots of two table structures.
department_id is foreign key in user table.When i delete the department ,the respective department_id in user table not getting deleted.it stays as assigned before.How can i delete the department_id in users table.

Actually, cakephp does not provide a mean to do that. You need to set SET NULL ON DELETE in the database.

Related

Database join vs keeping column in single table

I have two tables. One is Employee and another is Role. The Employee table contains EmployeeId and EmployeeName. Similarly, the Role table will contain RoleId and RoleName column. An employee will belong to one of several different roles.
I am new to database design and having problem figuring out if it's better to have a RoleId column in the Employee table or to create another table called EmployeeRoleMapping which will contain rows indicating this employee is mapped to this role. What are the pros and cons of both approach?
the most important thing is that you must sure of the relationship between Employee and Role in your application.
In my point of view, an Employee may have one or many roles and vise versa one role may belong to one or many Employees. So that, we have to create a new table called EmployeeRoles and this table has employeed_id and Role_id.

DB Design: user and role tables and/or specific user type tables?

I have a specific application where users could have different roles. Apart from different permissions controlled by the business logic, certain roles of users need additional linked info. For example, the Requestors are linked to departments.
So the question is which is the better way to go aa far as the design goes:
1
User table with department_id linked to the Department table that will be null for non requestors.
Role table with user roles
UserRole linking table
2
User table
Requestor table with department_id linked to departments and user_id linked to users
Role table with user roles
UserRole table
So, in other words, for the special types of roles that might need to have or be linked to additional data, do I create separate tables, or bunch all together inside the user table and handle the rest via the roles and the application logic?
Hope all the user can't be requester. Also If you link dept and user then dept_id will be FK in user table that wont be good. Also in future if you need to link some other info then you have add another attribute in user table. Bridge table between user and department will be good same way like userRole bridge table. If the user is requester then bridge table will have record for that user (user_id and dept_id). In future if you want to create different link then you can create another bridge table and maintain instead of adding attribute in user table

Cakephp 2 user have multiple group access

How to change cakephp concept user belongsTo Group, to user hasMany Group,
apart make new table called users_groups, and what should I change to make my user really have access to many groups that user have?
Make Things Simple:
In such scenario you already needed three tables.
Users table
id
name
phone
Groups
id
group(title)
User_Groups
id
user_id
group_id
As you need relations like user has many groups then you need extra table which at least should include foreign key like user_id, and group_id.So don't hesitate to add extra table for making things simple.
You'd be using 3 tables, and then a HasAndBelongsToMany relationship over the Users_Groups. Users HABTM Groups

cakephp Database Design

My data structure is as follows:
Company hasMany Regions
Region hasMany Markets
Market hasMany Stores
Store hasMany Employees
I also have the appropriate belongsTo where necessary.
I used foreign keys for associations. For example, each store has market_id.
When I delete a Company record, the correct Region is also deleted. However, it occurred to me that I also need all associated Markets, Stores, and Employees to be deleted. Or if I deleted a Market, I would need all Stores and Employees deleted.
What is the most appropriate manner of accomplishing this?
Would I add additional foreign keys to the tables? For example, would Stores need region_id and company_id in addition to market_id?
Use dependent association:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany
dependent: When dependent is set to true, recursive model deletion is
possible. In this example, Comment records will be deleted when their
associated User record has been deleted.
You don't need to add the additional foreign keys.

Generalization in Database

I Need to Design a database for a system where there's Customers and Vendors but they both are related to entity called Users where every user is either a customer or a vendor .
the problem is that Vendors are related to other entities that Customers aren't .
so how can I design such a database ?
The other entities will store the ID of the Vendor as a foreign key. And Vendors and Customers are not going to be in the same table anyway*, so it's not like the two have IDs that might be used at the same time for that.
Also, to add, the Foreign Key you require for User could be managed as an add/edit trigger if your DB of choice allows it. That way you can make sure that the Vendor id used for those related entities isn't a User ID linked to Customers. (...WHERE userid NOT IN (SELECT userid FROM users WHERE customer = TRUE))
* Customers and Vendors have different properties/fields so shouldn't be in the same table.
You could have Vendors and Customers have a relationship to a User table.
user
===========
userId
name
vendor
===========
vendorId
companyName
userId
customer
===========
customerId
source
userId
Then you can link to both customers and vendors from the same table, yet they can still share the same common data in the user table. In fact, a customer could also be a vendor.
Your question could be generalized as follows: how do I express subclasses in relational tables?
For the generic answer, see this:
https://stackoverflow.com/tags/class-table-inheritance/info

Resources