Completed
Push — master ( dbd6b1...1c86dd )
by Jared
04:09
created

BelongsToMany::getResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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 BelongsToMany extends Relation
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $tablename;
21
22
    /**
23
     * @param string $model      foreign model class
24
     * @param string $tablename  pivot table name
25
     * @param string $foreignKey identifying key on foreign model
26
     * @param string $localKey   identifying key on local model
27
     * @param Model  $relation
28
     */
29
    public function __construct($model, $tablename, $foreignKey, $localKey, Model $relation)
30
    {
31
        parent::__construct($model, $foreignKey, $localKey, $relation);
32
33
        $this->tablename = $tablename;
34
    }
35
36 View Code Duplication
    protected function initQuery()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
37
    {
38
        // set join
39
        // TODO
40
41
        $ids = $this->relation->ids();
42
        foreach ($ids as $property => $id) {
43
            if ($id === null) {
44
                $this->empty = true;
45
            }
46
47
            $this->query->where($this->foreignKey, $id);
48
        }
49
    }
50
51
    public function getResults()
52
    {
53
        if ($this->empty) {
54
            return;
55
        }
56
57
        return $this->query->execute();
58
    }
59
60
    /**
61
     * Gets the pivot tablename.
62
     *
63
     * @return string
64
     */
65
    public function getTablename()
66
    {
67
        return $this->tablename;
68
    }
69
70
    public function create(array $values = [])
71
    {
72
        $class = $this->model;
73
        $model = new $class($values);
74
        $model->save();
75
76
        // create pivot relation
77
        $pivot = new Pivot();
78
        $pivot->setTablename($this->tablename);
79
80
        $ids = $model->ids();
81
        foreach ($ids as $property => $id) {
82
            $pivot->{$this->localKey} = $id;
83
        }
84
85
        $ids = $this->relation->ids();
86
        foreach ($ids as $property => $id) {
87
            $pivot->{$this->foreignKey} = $id;
88
        }
89
90
        $pivot->save();
91
        $model->pivot = $pivot;
92
93
        return $model;
94
    }
95
}
96