1: <?php
2:
3: namespace PHPixie\ORM;
4:
5: class Relationships
6: {
7: protected $ormBuilder;
8: protected $relationships = array();
9: protected $classMap = array(
10: 'oneToOne' => '\PHPixie\ORM\Relationships\Type\OneTo\Type\One',
11: 'oneToMany' => '\PHPixie\ORM\Relationships\Type\OneTo\Type\Many',
12: 'manyToMany' => '\PHPixie\ORM\Relationships\Type\ManyToMany',
13: 'embedsOne' => '\PHPixie\ORM\Relationships\Type\Embeds\Type\One',
14: 'embedsMany' => '\PHPixie\ORM\Relationships\Type\Embeds\Type\Many',
15: );
16:
17: public function __construct($ormBuilder)
18: {
19: $this->ormBuilder = $ormBuilder;
20: }
21:
22: public function get($name)
23: {
24: if (!array_key_exists($name, $this->relationships))
25: {
26: $this->relationships[$name] = $this->buildRelationship($name);
27: }
28:
29: return $this->relationships[$name];
30: }
31:
32: 33: 34: 35: 36:
37: protected function buildRelationship($name) {
38: if(!array_key_exists($name, $this->classMap)) {
39: throw new \PHPixie\ORM\Exception\Relationship("Relationship type '$name' does not exist");
40: }
41:
42: $class = $this->classMap[$name];
43: return new $class(
44: $this->ormBuilder->configs(),
45: $this->ormBuilder->models(),
46: $this->ormBuilder->planners(),
47: $this->ormBuilder->plans(),
48: $this->ormBuilder->steps(),
49: $this->ormBuilder->loaders(),
50: $this->ormBuilder->mappers()
51: );
52: }
53: }
54: