Completed
Push — master ( 9b95b7...985d72 )
by Jared
02:36
created

HasMany::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace Pulsar\Relation;
12
13
use Pulsar\Model;
14
15
class HasMany extends Relation
16
{
17
    protected function initQuery()
18
    {
19
        $localKey = $this->localKey;
20
        $value = $this->localModel->$localKey;
21
22
        if ($value === null) {
23
            $this->empty = true;
24
        }
25
26
        $this->query->where($this->foreignKey, $value);
27
    }
28
29
    public function getResults()
30
    {
31
        if ($this->empty) {
32
            return;
33
        }
34
35
        return $this->query->execute();
36
    }
37
38
    public function save(Model $model)
39
    {
40
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
41
        $model->save();
42
43
        return $model;
44
    }
45
46
    public function create(array $values = [])
47
    {
48
        $class = $this->foreignModel;
49
        $model = new $class();
50
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
51
        $model->create($values);
52
53
        return $model;
54
    }
55
56
    /**
57
     * Attaches a child model from this model.
58
     *
59
     * @param Model $model child model
60
     *
61
     * @return self
62
     */
63
    public function attach(Model $model)
64
    {
65
        $model->{$this->foreignKey} = $this->localModel->{$this->localKey};
66
        $model->save();
67
68
        return $this;
69
    }
70
71
    /**
72
     * Detaches a child model from this model.
73
     *
74
     * @param Model $model child model
75
     *
76
     * @return self
77
     */
78
    public function detach(Model $model)
79
    {
80
        $model->{$this->foreignKey} = null;
81
        $model->save();
82
83
        return $this;
84
    }
85
86
    /**
87
     * Removes any relationships that are not included
88
     * in the list of IDs.
89
     * 
90
     * @param array $ids
91
     *
92
     * @return self
93
     */
94
    public function sync(array $ids)
95
    {
96
        $class = $this->foreignModel;
97
        $model = new $class();
98
        $query = $model::query();
99
100 View Code Duplication
        if (count($ids) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
            $in = implode(',', $ids);
102
            $query->where("{$this->foreignKey} NOT IN ($in)");
103
        }
104
105
        $query->delete();
106
107
        return $this;
108
    }
109
}
110