Completed
Push — master ( 529f36...77a63f )
by Restu
13:12
created

Model::save()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 11
c 2
b 0
f 1
nc 4
nop 0
dl 0
loc 20
rs 8.8571
1
<?php
2
namespace JayaCode\Framework\Core\Database\Model;
3
4
use JayaCode\Framework\Core\Database\Database;
5
6
/**
7
 * Class Model
8
 * @package JayaCode\Framework\Core\Database\Model
9
 */
10
abstract class Model
11
{
12
    /**
13
     * @var Database
14
     */
15
    public static $db;
16
17
    /**
18
     * @var string
19
     */
20
    protected $primaryKey = "id";
21
22
    /**
23
     * @var null
24
     */
25
    protected static $table = null;
26
27
    /**
28
     * @var array
29
     */
30
    protected $data = array();
31
32
    /**
33
     * @var bool
34
     */
35
    protected $isNewRow = true;
36
37
    /**
38
     * @param array $data
39
     * @param bool $isNewRow
40
     */
41
    public function __construct($data = array(), $isNewRow = true)
42
    {
43
        $this->data = $data;
44
        $this->isNewRow = $isNewRow;
45
    }
46
47
    /**
48
     * @param array $data
49
     * @return static
50
     */
51
    public static function create($data = array())
52
    {
53
        return new static($data);
54
    }
55
56
    /**
57
     * @param null $columns
58
     * @return Database
59
     */
60
    public static function select($columns = null)
61
    {
62
        static::$db->setModel(get_class(new static()), static::$table);
63
        return static::$db->select($columns);
64
    }
65
66
    /**
67
     * @param $name
68
     * @return array
69
     */
70
    public function __get($name)
71
    {
72
        return arr_get($this->data, $name);
73
    }
74
75
    /**
76
     * @param $name
77
     * @param $value
78
     */
79
    public function __set($name, $value)
80
    {
81
        $this->data[$name] = $value;
82
    }
83
84
    /**
85
     *
86
     */
87
    public function delete()
88
    {
89
        //TODO: will be implemented after query builder delete finish
90
    }
91
92
    /**
93
     *
94
     */
95
    public function update()
96
    {
97
        //TODO: will be implemented after query builder update finish
98
    }
99
100
    /**
101
     *
102
     */
103
    public function save()
104
    {
105
        //TODO: will be implemented after query builder insert & update finish
106
107
        static::$db->setModel(get_class(new static()), static::$table);
108
        if ($this->isNewRow && static::$db->insert($this->data)) {
109
            $this->isNewRow = false;
110
111
            if ($lastInsertID = static::$db->lastInsertId()) {
112
                $newData = static::$db->table(static::$table)
113
                    ->select()
114
                    ->where($this->primaryKey, $lastInsertID)
115
                    ->first();
116
117
                $this->data = $newData?$newData:$this->data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $newData ? $newData : $this->data of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
118
            }
119
        }
120
121
        return $this;
122
    }
123
}
124