HasOneOrMany   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 59
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getParentKey() 0 3 1
A get() 0 3 1
A addConstraints() 0 5 2
1
<?php
2
/**
3
 * Framy Framework
4
 *
5
 * @copyright Copyright Framy
6
 * @Author Marco Bier <[email protected]>
7
 */
8
9
namespace app\framework\Component\Database\Model\Relations;
10
11
use app\framework\Component\Database\Model\Model;
12
use app\framework\Component\Database\Model\Builder;
13
14
abstract class HasOneOrMany extends Relation
15
{
16
    /**
17
     * The foreign key of the parent model.
18
     *
19
     * @var string
20
     */
21
    protected $foreignKey;
22
23
    /**
24
     * The local key of the parent model.
25
     *
26
     * @var string
27
     */
28
    protected $localKey;
29
30
    /**
31
     * Create a new has one or many relationship instance.
32
     *
33
     * @param  Builder  $query
34
     * @param  Model  $parent
35
     * @param  string  $foreignKey
36
     * @param  string  $localKey
37
     * @return void
38
     */
39
    public function __construct(Builder $query, Model $parent, $foreignKey, $localKey)
40
    {
41
        $this->localKey   = $localKey;
42
        $this->foreignKey = $foreignKey;
43
44
        parent::__construct($query, $parent);
45
    }
46
47
    /**
48
     * Set the base constraints on the relation query.
49
     *
50
     * @return void
51
     */
52
    public function addConstraints()
53
    {
54
        if (static::$constraints) {
55
            $this->query->where($this->foreignKey, '=', $this->getParentKey());
56
            $this->query->whereNotNull($this->foreignKey);
57
        }
58
    }
59
60
    /**
61
     * Get the key value of the parent's local key.
62
     *
63
     * @return mixed
64
     */
65
    public function getParentKey()
66
    {
67
        return $this->parent->getAttribute($this->localKey);
68
    }
69
70
    public function get(array $columns = ['*'])
71
    {
72
        return $this->query->get($columns);
73
    }
74
}
75