Jose
Jose

Reputation: 1

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?

  1. Would I add additional foreign keys to the tables? For example, would Stores need region_id and company_id in addition to market_id?

Upvotes: 0

Views: 325

Answers (1)

ihsan
ihsan

Reputation: 2289

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.

Upvotes: 1

Related Questions