1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Models\Sql\Relations; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Database\DataObjects\Result; |
19
|
|
|
use O2System\Reactor\Models\Sql; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class HasOne |
23
|
|
|
* |
24
|
|
|
* @package O2System\Reactor\Models\Sql\Relations |
25
|
|
|
*/ |
26
|
|
|
class HasOne extends Sql\Relations\Abstracts\AbstractRelation |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Get Result |
30
|
|
|
* |
31
|
|
|
* @return null |
32
|
|
|
*/ |
33
|
|
|
public function getResult() |
34
|
|
|
{ |
35
|
|
|
if ($this->map->referenceModel->row instanceof Sql\DataObjects\Result\Row) { |
|
|
|
|
36
|
|
|
|
37
|
|
|
$criteria = $this->map->referenceModel->row->offsetGet($this->map->referenceModel->primaryKey); |
38
|
|
|
$conditions = [$this->map->relationForeignKey => $criteria]; |
|
|
|
|
39
|
|
|
|
40
|
|
|
if ($this->map->relationModel instanceof Sql\Model) { |
|
|
|
|
41
|
|
|
$result = $this->map->relationModel->qb |
42
|
|
|
->from($this->map->relationTable) |
43
|
|
|
->getWhere($conditions, 1); |
44
|
|
|
|
45
|
|
|
if ($result instanceof Result) { |
|
|
|
|
46
|
|
|
if ($result->count() > 0) { |
47
|
|
|
$this->map->relationModel->result = new Sql\DataObjects\Result($result, |
48
|
|
|
$this->map->relationModel); |
49
|
|
|
|
50
|
|
|
return $this->map->relationModel->row = $this->map->relationModel->result->first(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} elseif ( ! empty($this->map->relationTable)) { |
54
|
|
|
$result = $this->map->referenceModel->qb |
55
|
|
|
->from($this->map->relationTable) |
56
|
|
|
->getWhere($conditions, 1); |
57
|
|
|
|
58
|
|
|
if ($result instanceof Result) { |
59
|
|
|
if ($result->count() > 0) { |
60
|
|
|
$result = new Sql\DataObjects\Result($result, $this->map->referenceModel); |
61
|
|
|
|
62
|
|
|
return $result->first(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |