1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Relation; |
4
|
|
|
|
5
|
|
|
use Rougin\Windstorm\MixedInterface; |
6
|
|
|
use Rougin\Windstorm\RelationInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* One-To-Many Relation |
10
|
|
|
* |
11
|
|
|
* @package Windstorm |
12
|
|
|
* @author Rougin Gutib <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class OneToMany extends Relation implements RelationInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $column; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Sets the column name. |
23
|
|
|
* |
24
|
|
|
* @param string $column |
25
|
|
|
* @return self |
26
|
|
|
*/ |
27
|
6 |
|
public function column($column) |
28
|
|
|
{ |
29
|
6 |
|
$this->column = $column; |
30
|
|
|
|
31
|
6 |
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Generates the query instance from relation. |
36
|
|
|
* |
37
|
|
|
* @param string $primary |
38
|
|
|
* @param string $foreign |
39
|
|
|
* @return \Rougin\Windstorm\QueryInterface |
40
|
|
|
*/ |
41
|
6 |
|
public function make($primary, $foreign) |
42
|
|
|
{ |
43
|
6 |
|
$this->field(1, (string) $foreign); |
44
|
|
|
|
45
|
6 |
|
$query = clone $this->query; |
46
|
|
|
|
47
|
6 |
|
$query = $query->select((array) $this->columns(0)); |
48
|
|
|
|
49
|
6 |
|
$query->from($this->primary, (string) $this->alias[0]); |
50
|
|
|
|
51
|
6 |
|
$mixed = new Mixed($query); |
52
|
|
|
|
53
|
6 |
|
return $this->child($mixed, $primary, $foreign); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Generates a child instance from the foreign table. |
58
|
|
|
* |
59
|
|
|
* @param \Rougin\Windstorm\MixedInterface $mixed |
60
|
|
|
* @param string $primary |
61
|
|
|
* @param string $foreign |
62
|
|
|
* @return \Rougin\Windstorm\MixedInterface |
63
|
|
|
*/ |
64
|
6 |
|
protected function child(MixedInterface $mixed, $primary, $foreign) |
65
|
|
|
{ |
66
|
6 |
|
$child = clone $this->query; |
67
|
|
|
|
68
|
6 |
|
$child = $child->select($this->columns(1, false)); |
69
|
|
|
|
70
|
6 |
|
$child->from($this->foreign, $this->alias[1]); |
71
|
|
|
|
72
|
6 |
|
$mixed->add($this->column, $child, $primary, $foreign); |
73
|
|
|
|
74
|
6 |
|
return $mixed; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|