addRelationship()

addRelationship($relName,$relType,$foreignClass, $thisKey, $relKey = null, $outKey = null, $relOutKey = null, $relTable = null, $lazy = false)

This method creates a relationship field into the mapping.
Relationship fields has different behavior than the column fields they comunicate with another PhpBURN models connecting them by some
kind of reference.

@param String $relName
@param int $relType
@param String $foreignClass
@param String $thisKey
@param String $relKey
@param String $outKey
@param String $relOutKey
@param String $relTable
@param Boolean $lazy
@return PhpBURN_Map

Params


$relName
This is how you will call your relationship such as “albums, pictures, comments, rate, tags, etc”

$relType
It ALWAYS refers to self:: static constants:
self::ONE_TO_ONE
self::ONE_TO_MANY
self::MANY_TO_MANY

$foreignClass
Name of the class you will reference to. It should be ALWAYS a mapped PhpBURN Model in any package.

$thisKey
Any reference should be at least between two fields, thisKey as it say reference to the field alias on the current Model that will reference with the field alias in the external Class/Model.

$relKey
It has two different usages.
Using with self::ONE_TO_ONE or self::ONE_TO_MANY:
In this case it will reference to the external class field alias

Using with self::MANY_TO_MANY:
In this case it will be the $relTable column that references with $thisKey.

$outKey
Used only in self::MANY_TO_MANY cases. This represents the external class field wich will be used to compare with $relTable column.

$relOutKey
Used only in self::MANY_TO_MANY cases. This represents the $relTable column that references to $outKey

$relTable
Used only in self::MANY_TO_MANY cases. This is the reference table that connects one model to another, that don’t need to be mapped as a PhpBURN Model, it must only exists in the database with the apropriated database relationships.

Explanation


Relationships used to be a painful thing to developers to handle with, but now it became a very simple thing, all you need is create the tables, map the model and say that is a relationship and start using transparently.

Here we will see a detailed explanation of how does Relationships works and how to set them.

Lets assume the following database structure:

EER Diagram

EER Diagram

In simple words:

Users has many Albums ( self::ONE_TO_MANY )
Albums has many Pictures ( self::ONE_TO_MANY )
many Albums has many Tags ( self::MANY_TO_MANY )
many Pictures has many Tags ( self::MANY_TO_MANY )

So in this case we have 6 ( six ) Tables and 3 ( three ) Models. I will not be so deep explain relationships because you can find it at Tutorial area in www.phpburn.com/tutorials.

So how will our code be into our models?

Well, first of all you can create relationships on-the-fly but we recommend you to create it at _mapping() magic method at PhpBURN Model.

According to the figure above:

Users.php

public function _mapping() {

...
...
...

$this->getMap()->addRelationship('albums',self::ONE_TO_MANY,'Albums','id_user','user');

}

Albums.php

public function _mapping() {

...
...
...

$this->getMap()->addRelationship('pictures',self::ONE_TO_MANY,'Pictures','id_album','album');
$this->getMap()->addRelationship('tags',self::MANY_TO_MANY,'Tags','id_album','album','id_tag','tag','albums_has_tags');

}

Pictures.php

public function _mapping() {

...
...
...

$this->getMap()->addRelationship('tags',self::MANY_TO_MANY,'Tags','id_picture','picture','id_tag','tag','pictures_has_tags');

}

WOW!!!!!!! That’s it? YEAH!!! That is it… pretty easy uh? That’s all you need to put this complex relationship structure working for you without any more troubles.

Relationships can be fun, BE CREATIVE.