Jump to content

ATK Relations: Difference between revisions

From NusaATK
m Relations moved to ATK Reference Relations
No edit summary
 
(19 intermediate revisions by 5 users not shown)
Line 1: Line 1:
A relation defines a relation between two tables. One or more records in the first table belong to one or more records in the second table. A one to many relation would be '1 library has N books'.
A relation defines a relation between two tables. One or more records in the first table belong to one or more records in the second table. A one to many relation would be '1 library has N books'. Below is a list of the relations that are currently available in ATK.


= atkOneToOneRelation =
{| border=1 cellpadding=5
An atkOneToOneRelation defines a relation between two tables where there is one record in the first table that belongs to one record in the second table.  
|- style="background-color: #FFFACD;"
! Relation || Description || Screenshot
|-
| [[atkOneToOneRelation]] || 'A person has one address' ||
|-
| [[atkOneToManyRelation]] || 'A department has many employees' || [[Image:atkOneToManyRelation.png]]
|-
| [[atkManyToOneRelation]] || 'A project has a projectleader (that can lead multiple projects)' || [[Image:atkManyToOneRelation.png]]
|-
| [[atkManyToManyRelation]] || 'A shopper buys multiple products, a product can be bought by multiple shoppers' ||
|-
| [[atkManyToManySelectRelation]] || 'A shopper buys multiple products, a product can be bought by multiple shoppers' ||
|-
| [[atkManyBoolRelation]] || 'An article has several categories, indicated with checkboxes' || [[Image:atkManyBoolRelation.png]]
|-
| [[atkManyToOneTreeRelation]] || 'A category has a parent category' || [[Image:atkManyToOneTreeRelation.png]]
|-
| [[atkMatrixRelation]] || 'In a project, phases x and y depend on phases a and b' ||
|-
| [[atkSecureRelation]] || 'A patient has personal information, that must be secured' ||
|-
| [[atkShuttleRelation]] || 'An operator operates in several countries, selected from a list' || [[Image:atkShuttleRelation.png]]
|-
| [[atkExtendableShuttleRelation]] || 'An operator operates in several countries, selected from a list which can be filtered' ||
|}


The atkOneToOneRelation supports two configurations: master mode and slave mode. The mode to use is detected automatically based on the value of the $refKey parameter.
Relations behave similar to [[Attributes]] (in fact, relationships are nothing more then special attributes).
 
function atkOneToOneRelation($name, $destination, $refKey="", $flags=0)
 
* $name: The unique name of the attribute.
* $destination: the destination node (in module.nodename notation)
* $refKey: foreign-key field (master mode) or empty (slave mode)
 
== Master mode example ==
The node in which the relation is implemented, is considered the master (code is in node A). And the referential key pointing to the master record (a_id) is in the destination node (node B).
 
[[image:atkOneToOne_master.jpg|center]]
 
Referential key a_id is in the other node, so this is the master-configuration.
 
== Slave mode example ==
The node in which the relation is implemented (code is in node B), is considered the child. And the referential key pointing to the master record (a_id) is in the current node (node B).
 
[[image:atkOneToOne_slave.jpg|center]]
 
Referential key a_id is in the same node, so this is the slave-configuration.
 
= atkOneToManyRelation =
function atkOneToManyRelation($name, $destination, $refKey="", $flags=0)
* $name The unique name of this relation within a node ''- In contrast with moth other attributes, the name does not correspond to a database field. This is because in one2many relations, the databasefield that stores the link, is in the destination node (B) and not in the owner node (A)''
* $destination The node to which the relationship is made
* $refKey For regular oneToMany relationships, $refKey is name of the referential key in the destination node. In the case of multi-foreign key relationships, $refKey can be an array of fields.
 
 
'''Example'''
 
Suppose a department (node A) has many employees (node B). To edit the list of employees in a department, this relationship can be built with an atkOneToManyRelation. Each employee gets a department_id (a_id).
 
[[image:atkOneToMany.jpg|center]]
 
= atkManyToOneRelation =
function atkManyToOneRelation($name, $destination, $flags=0)
 
* $name The name of the attribute.
* $destination The node we have a relationship with.
 
 
'''Example'''
 
Projects all have one coordinator (node B), but one coordinator can have multiple projects (node A). So in the project class, there's a atkManyToOneRelation to a coordinator.
 
[[image:atkManyToOne.jpg|center]]
 
= Many-To-Many-Relation =
 
This relation can be created with an intermediate table to store the relations.
 
A Many-To-Many-Relation basically consist of a atkOneToMany relation to the intermediate table. The intermediate node has two atkManyToOne relations defined.
 
 
'''Example'''
 
One person (node A) can have several skills (node B) and the same skill applies to more than one person.
 
[[image:atkManyToMany.jpg|center]]
 
 
If, in the example above, a skill is removed, this skill must be removed for all employees. This can be achieved by adding the AF_CASCADE_DELETE flag to the atkOneToManyRelation in node B:
 
atkOneToMany(“unique dummy”,
              “mymod.A_B”,
              “b_id”,
              AF_HIDE|AF_CASCADE_DELETE);
 
Normally you assign skills to a person (an not the other way round), so the atkOneToManyRelation in this node must be hidden.
 
 
And on the other hand, if an employee is deleted, the correspoding record(s) in the intermediate table must be removed as well.
 
atkOneToMany(“unique dummy”,
              “mymod.A_B”,
              “a_id”,
              AF_CASCADE_DELETE);

Latest revision as of 10:44, 8 March 2010

A relation defines a relation between two tables. One or more records in the first table belong to one or more records in the second table. A one to many relation would be '1 library has N books'. Below is a list of the relations that are currently available in ATK.

Relation Description Screenshot
atkOneToOneRelation 'A person has one address'
atkOneToManyRelation 'A department has many employees'
atkManyToOneRelation 'A project has a projectleader (that can lead multiple projects)'
atkManyToManyRelation 'A shopper buys multiple products, a product can be bought by multiple shoppers'
atkManyToManySelectRelation 'A shopper buys multiple products, a product can be bought by multiple shoppers'
atkManyBoolRelation 'An article has several categories, indicated with checkboxes'
atkManyToOneTreeRelation 'A category has a parent category'
atkMatrixRelation 'In a project, phases x and y depend on phases a and b'
atkSecureRelation 'A patient has personal information, that must be secured'
atkShuttleRelation 'An operator operates in several countries, selected from a list'
atkExtendableShuttleRelation 'An operator operates in several countries, selected from a list which can be filtered'

Relations behave similar to Attributes (in fact, relationships are nothing more then special attributes).