Changeset 5287
- Timestamp:
- 12/11/08 15:09:22 (7 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/1.1/docs/manual/en/basic-schema-mapping/record-identifiers.txt
r4674 r5287 8 8 +++ Natural 9 9 10 Natural identifier is a property or combination of properties that is unique and non-null. The use of natural identifiers 11 is encouraged. 10 Natural identifier is a property or combination of properties that is unique and non-null. 12 11 13 12 <code type="php"> … … 31 30 32 31 +++ Autoincremented 33 Autoincrement primary key is the most basic identifier and its usage is strongly encouraged. Sometimes you may want to use32 Autoincrement primary key is the most basic identifier. Sometimes you may want to use 34 33 some other name than {{id}} for your autoinc primary key. It can be specified as follows: 35 34 … … 56 55 </code> 57 56 58 You should consider using autoincremented or sequential primary keys only when the record cannot be identified naturally59 (in other words it doesn't have a natural identifier).60 61 The following example shows why natural identifiers are more efficient.62 63 Consider three classes Permission, Role and RolePermission. Roles having many permissions and vice versa (so their64 relation is many-to-many). Now lets also assume that each role and permission are naturally identified by their names.65 66 Now adding autoincremented primary keys to these classes would be simply stupid. It would require more data and it would67 make the queries more inefficient. For example fetching all permissions for role 'Admin' would be done as follows68 (when using autoinc pks):69 70 <code type="sql">71 SELECT p.*72 FROM Permission p73 LEFT JOIN RolePermission rp ON rp.permission_id = p.id74 LEFT JOIN Role r ON rp.role_id = r.id75 WHERE r.name = 'Admin'76 </code>77 78 Now remember sql JOINS are always expensive and here we are using two of those. When using natural identifiers the query79 would look like:80 81 <code type="sql">82 SELECT p.*83 FROM Permission p84 LEFT JOIN RolePermission rp ON rp.permission_name = p.name85 WHERE rp.role_name = 'Admin'86 </code>87 88 Thats -1 JOIN !89 90 57 +++ Composite 91 58 92 Composite primary key can be used efficiently in association tables (tables that connect two components together). It is 93 not recommended to use composite primary keys in anywhere else as Doctrine does not support mapping relations on multiple 94 columns. 95 96 Due to this fact your doctrine-based system will scale better if it has autoincremented primary key even for association 97 tables. 59 A composite primary key is often used in association tables for example (tables that connect two components together). It is 60 currently not recommended to use composite primary keys in anywhere else as Doctrine does not support mapping relations on multiple 61 columns. This will be supported in a future version. 98 62 99 63 <code type="php">