thekid/rfc — Issue #96: RFC 0096: Foreign key relations
Author: thekid
Created: 2011-10-09 15:02:22+00:00
State: open

Scope of Change
====
The DataSet classes will be enhanced to know about
- foreign keys.
- foreign keys, they are target of.

Rationale
====
- Easy access to related data
- Create "Rails-like" applications

Functionality
====

The DataSet Class Generator has also collect data about foreign keys.
Therefor the database adapter classes have to be rewritten.

A naming strategy is necessary to avoid collisions. Also, the nameing
strategy has to be replaceable.

With this two problems solved you can generate access methods for each
of a DataSet's related DataSet.

Advantage
----

former code:

```php
getChief_id());
?>
```

new code:

```php
getDepartmentChief();
?>
```

usage example
----

```php
register(
DriverManager::getConnection('mysql://test:test@localhost/?autoconnect=1')
);

$chief= Department::getDepartmentById(1)->getDepartmentChief();
var_dump($chief);
?>
```

```
object(Person)#75 (2) {
["person_id"]=>
int(5)
["name"]=>
string(14) "Max Mustermann"
}

```

table view
----

```
+---------------+
| person |
+---------------+ +
| person_id (P) |<------+
| name | |
+---------------+ |
| person_id = chief_id
|
|
| +-------------------+
| 1 | department |
+---+-------------------+
| department_id (P) |
| chief_id (I) |
+-------------------+

```

generated classes
----

```
+----------------------------------------+
| Person |
+----------------------------------------+
| + person_id |
| + name |
+----------------------------------------+
| int getPerson_id() |
| string getName() |
| Person getPersonById($id) |
| Department[] getDepartmentList() |
| ResultIterator getDepartmentIterator() |
+----------------------------------------+

+-----------------------------------+
| Department |
+-----------------------------------+
| + department_id |
| + chief_id |
+-----------------------------------+
| int getDepartment_id() |
| string getChief_id() |
| Department getDepartmentById($id) |
| Person getDepartmentChief() |
+-----------------------------------+

```

naming strategy
----
- The strategy has to implement the abstract class rdbms.util.DBXMLNamingStrategy.
- To use a self written strategy you can set it by:

```php

```

- If nothing is set the rdbms.util.DBXMLNamingStrategyDefault will be used.

Security considerations
====
n/a

Speed impact
====
This extension has no influence to the former functionality and runspeed

Dependencies
====
Note:

```
To use this API, the database classes have to be regenerated!

```

Related documents
====
- javax.persistence package:
http://java.sun.com/javaee/5/docs/api/javax/persistence/ManyToMany.html
http://java.sun.com/javaee/5/docs/api/javax/persistence/OneToMany.html
http://java.sun.com/javaee/5/docs/api/javax/persistence/OneToOne.html
- xp-lab
http://experiments.xp-framework.net/?people,wagner,rfc,0096
- Joint patch for RFC0096, RFC0123, RFC0124 and RFC0129 against skeleton
http://xp-framework.net/rfc/contrib/rfc0096_0123_0124_0129/skeleton.diff
- Joint patch for RFC0096, RFC0123, RFC0124 and RFC0129 against ports
http://xp-framework.net/rfc/contrib/rfc0096_0123_0124_0129/ports.diff

Comments
====