1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2014-2018 James Ekow Abaka Ainooson |
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
|
|
|
/** |
30
|
|
|
* Relationships provide queries for fetching data from related models when using lazy loading. |
31
|
|
|
*/ |
32
|
|
|
abstract class Relationship |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Constant for the belongs-to or one-to many relation ship. |
36
|
|
|
*/ |
37
|
|
|
const BELONGS_TO = 'BelongsTo'; |
38
|
|
|
|
39
|
|
|
const HAS_MANY = 'HasMany'; |
40
|
|
|
const MANY_HAVE_MANY = 'ManyHaveMany'; |
41
|
|
|
|
42
|
|
|
protected $options = []; |
43
|
|
|
protected $type; |
44
|
|
|
protected $setupName; |
45
|
|
|
protected $setupTable; |
46
|
|
|
protected $setupSchema; |
47
|
|
|
protected $setupPrimaryKey; |
48
|
|
|
|
49
|
|
|
private $setup = false; |
50
|
|
|
private $query; |
51
|
|
|
protected $queryPrepared = false; |
52
|
|
|
protected $invalidFields = []; |
53
|
|
|
|
54
|
24 |
|
public function setOptions($options) |
55
|
|
|
{ |
56
|
24 |
|
$this->options = $options; |
57
|
24 |
|
} |
58
|
|
|
|
59
|
8 |
|
public function createQuery() |
|
|
|
|
60
|
|
|
{ |
61
|
8 |
|
if (!$this->query) { |
62
|
8 |
|
$this->query = new QueryParameters(); |
63
|
|
|
} |
64
|
8 |
|
return $this->query; |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
public function getOptions() |
68
|
|
|
{ |
69
|
8 |
|
return $this->options; |
70
|
|
|
} |
71
|
|
|
|
72
|
8 |
|
private function initialize() |
73
|
|
|
{ |
74
|
8 |
|
if (!$this->setup) { |
75
|
8 |
|
$this->runSetup(); |
76
|
8 |
|
$this->setup = true; |
77
|
|
|
} |
78
|
8 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets an instance of the related model accessed through this class. |
82
|
|
|
* |
83
|
|
|
* @return RecordWrapper |
84
|
|
|
* @throws exceptions\NibiiException |
85
|
|
|
*/ |
86
|
8 |
|
public function getModelInstance() |
87
|
|
|
{ |
88
|
8 |
|
$this->initialize(); |
89
|
8 |
|
return ORMContext::getInstance()->getModelFactory()->createModel($this->options['model'], $this->type); |
90
|
|
|
} |
91
|
|
|
|
92
|
24 |
|
public function setup($name, $schema, $table, $primaryKey) |
93
|
|
|
{ |
94
|
24 |
|
$this->setupName = $name; |
95
|
24 |
|
$this->setupTable = $table; |
96
|
24 |
|
$this->setupPrimaryKey = $primaryKey; |
97
|
24 |
|
$this->setupSchema = $schema; |
98
|
24 |
|
} |
99
|
|
|
|
100
|
|
|
public function getInvalidFields() |
101
|
|
|
{ |
102
|
|
|
return $this->invalidFields; |
103
|
|
|
} |
104
|
|
|
|
105
|
8 |
|
public function prepareQuery($data) |
|
|
|
|
106
|
|
|
{ |
107
|
8 |
|
$this->initialize(); |
108
|
8 |
|
return $this->doPrepareQuery($data); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
abstract public function preSave(&$wrapper, $value); |
|
|
|
|
112
|
|
|
|
113
|
|
|
abstract public function postSave(&$wrapper); |
|
|
|
|
114
|
|
|
|
115
|
|
|
abstract protected function doPrepareQuery($data); |
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @todo Cleanup this method. |
119
|
|
|
* There should be a get Parameters method instead which returns the values |
120
|
|
|
* that are passed to setup. Initialize should be the main wrapper arround |
121
|
|
|
* this. |
122
|
|
|
* |
123
|
|
|
*/ |
124
|
|
|
abstract public function runSetup(); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
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.