1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 ekow. |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace ntentan\nibii; |
28
|
|
|
|
29
|
|
|
abstract class Relationship { |
30
|
|
|
|
31
|
|
|
const BELONGS_TO = 'BelongsTo'; |
32
|
|
|
const HAS_MANY = 'HasMany'; |
33
|
|
|
const MANY_HAVE_MANY = 'ManyHaveMany'; |
34
|
|
|
|
35
|
|
|
protected $options = []; |
36
|
|
|
protected $type; |
37
|
|
|
protected $setupName; |
38
|
|
|
protected $setupTable; |
39
|
|
|
protected $setupSchema; |
40
|
|
|
protected $setupPrimaryKey; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var \ntentan\panie\Container |
45
|
|
|
*/ |
46
|
|
|
protected $container; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @var ORMContext |
51
|
|
|
*/ |
52
|
|
|
protected $context; |
53
|
|
|
|
54
|
|
|
private $setup = false; |
55
|
|
|
private $query; |
56
|
|
|
protected $queryPrepared = false; |
57
|
|
|
|
58
|
28 |
|
public function setOptions($options) { |
59
|
28 |
|
$this->options = $options; |
60
|
28 |
|
} |
61
|
|
|
|
62
|
12 |
|
public function getQuery() { |
|
|
|
|
63
|
12 |
|
if(!$this->query) { |
64
|
12 |
|
$this->query = new QueryParameters(); |
65
|
|
|
} |
66
|
12 |
|
return $this->query; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getOptions() { |
70
|
|
|
return $this->options; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets an instance of the related model accessed through this class. |
75
|
|
|
* @return RecordWrapper |
76
|
|
|
*/ |
77
|
12 |
|
public function getModelInstance() { |
78
|
12 |
|
if (!$this->setup) { |
79
|
12 |
|
$this->runSetup(); |
80
|
12 |
|
$this->setup = true; |
81
|
|
|
} |
82
|
12 |
|
return $this->container->resolve($this->context->getClassName($this->options['model'], $this->type)); |
83
|
|
|
} |
84
|
|
|
|
85
|
28 |
|
public function setup($name, $schema, $table, $primaryKey) { |
86
|
28 |
|
$this->setupName = $name; |
87
|
28 |
|
$this->setupTable = $table; |
88
|
28 |
|
$this->setupPrimaryKey = $primaryKey; |
89
|
28 |
|
$this->setupSchema = $schema; |
90
|
28 |
|
} |
91
|
|
|
|
92
|
|
|
abstract public function prepareQuery($data); |
|
|
|
|
93
|
|
|
|
94
|
|
|
abstract public function runSetup(); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.