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\relationships; |
28
|
|
|
|
29
|
|
|
use ntentan\nibii\exceptions\FieldNotFoundException; |
30
|
|
|
use ntentan\nibii\ORMContext; |
31
|
|
|
use ntentan\nibii\Relationship; |
32
|
|
|
use ntentan\utils\Text; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Represents a one to many belongs to relationship. |
36
|
|
|
*/ |
37
|
|
|
class BelongsToRelationship extends Relationship |
38
|
|
|
{ |
39
|
|
|
protected $type = self::BELONGS_TO; |
40
|
|
|
|
41
|
4 |
|
public function prepareQuery($data) |
|
|
|
|
42
|
|
|
{ |
43
|
4 |
|
if (!array_key_exists($this->options['local_key'], $data)) { |
44
|
|
|
throw new FieldNotFoundException("Field {$this->options['local_key']} not found for belongs to relationship query."); |
45
|
|
|
} |
46
|
4 |
|
if (!isset($data[$this->options['local_key']])) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
4 |
|
$query = $this->getQuery(); |
50
|
4 |
View Code Duplication |
if ($this->queryPrepared) { |
51
|
4 |
|
$query->setBoundData($this->options['foreign_key'], $data[$this->options['local_key']]); |
52
|
|
|
} else { |
53
|
4 |
|
$query->setTable($this->getModelInstance()->getDBStoreInformation()['quoted_table']) |
54
|
4 |
|
->addFilter($this->options['foreign_key'], $data[$this->options['local_key']]) |
55
|
4 |
|
->setFirstOnly(true); |
56
|
4 |
|
$this->queryPrepared = true; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
return $query; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
public function runSetup() |
63
|
|
|
{ |
64
|
4 |
|
$model = ORMContext::getInstance()->getModelFactory()->createModel($this->options['model'], self::BELONGS_TO); |
65
|
4 |
|
$table = $model->getDBStoreInformation()['table']; |
66
|
4 |
|
if ($this->options['foreign_key'] == null) { |
67
|
4 |
|
$this->options['foreign_key'] = $model->getDescription()->getPrimaryKey()[0]; |
68
|
|
|
} |
69
|
4 |
|
if ($this->options['local_key'] == null) { |
70
|
4 |
|
$this->options['local_key'] = Text::singularize($table).'_id'; |
71
|
|
|
} |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
public function preSave(&$wrapper, $value) |
75
|
|
|
{ |
76
|
|
|
$value->save(); |
77
|
|
|
$wrapper[$this->options['local_key']] = $value[$this->options['foreign_key']]; |
78
|
|
|
unset($wrapper[$this->options['model']]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.