Completed
Push — master ( f7e861...136cb7 )
by Nekrasov
02:32
created

D7Model::save()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
nc 4
cc 4
eloc 11
nop 1
1
<?php
2
3
namespace Arrilot\BitrixModels\Models;
4
5
use Arrilot\BitrixModels\Queries\D7Query;
6
use Bitrix\Highloadblock\HighloadBlockTable;
7
use Exception;
8
9
class D7Model extends BaseBitrixModel
10
{
11
    /**
12
     * Constructor.
13
     *
14
     * @param $id
15
     * @param $fields
16
     */
17
    public function __construct($id = null, $fields = null)
18
    {
19
        $this->id = $id;
20
        
21
        $this->fill($fields);
22
    }
23
24
    /**
25
     * Instantiate a query object for the model.
26
     *
27
     * @return D7Query
28
     */
29
    public static function query()
30
    {
31
        return new D7Query(static::tableClass(), get_called_class());
32
    }
33
    
34
    /**
35
     * @return \Bitrix\Main\Entity\DataManager
36
     */
37
    public static function tableClass()
38
    {
39
        return HighloadBlockTable::compileEntity(HighloadBlockTable::getRowById(2))->getDataClass();
40
    }
41
42
    /**
43
     * Internal part of create to avoid problems with static and inheritance
44
     *
45
     * @param $fields
46
     *
47
     * @throws Exception
48
     *
49
     * @return static|bool
50
     */
51
    protected static function internalCreate($fields)
52
    {
53
        $model = new static(null, $fields);
54
        
55
        if ($model->onBeforeSave() === false || $model->onBeforeCreate() === false) {
56
            return false;
57
        }
58
59
        $bxObject = static::tableClass();
60
        $resultObject = $bxObject::add($fields);
61
        $result = $resultObject->isSuccess();
62
        if ($result) {
63
            $model->setId($resultObject->getId());
64
        }
65
66
        $model->onAfterCreate($result);
67
        $model->onAfterSave($result);
68
69
        if (!$result) {
70
            throw new Exception(implode('; ', $resultObject->getErrorMessages()));
71
        }
72
73
        return $model;
74
    }
75
76
    /**
77
     * Delete model
78
     *
79
     * @return bool
80
     */
81
    public function delete()
82
    {
83
        if ($this->onBeforeDelete() === false) {
84
            return false;
85
        }
86
87
        $bxObject = static::tableClass();
88
        $resultObject = $bxObject::delete($this->id);
89
        $result = $resultObject->isSuccess();
90
91
        $this->onAfterDelete($result);
92
93
        return $result;
94
    }
95
96
    /**
97
     * Save model to database.
98
     *
99
     * @param array $selectedFields save only these fields instead of all.
100
     *
101
     * @return bool
102
     */
103
    public function save($selectedFields = [])
104
    {
105
        $selectedFields = is_array($selectedFields) ? $selectedFields : func_get_args();
106
107
        if ($this->onBeforeSave() === false || $this->onBeforeUpdate() === false) {
108
            return false;
109
        }
110
111
        $fields = $this->normalizeFieldsForSave($selectedFields);
112
        $bxObject = static::tableClass();
113
        $resultObject = $bxObject::update($this->id, $fields);
114
        $result = $resultObject->isSuccess();
115
116
        $this->onAfterUpdate($result);
117
        $this->onAfterSave($result);
118
119
        return $result;
120
    }
121
    
122
    /**
123
     * Determine whether the field should be stopped from passing to "update".
124
     *
125
     * @param string $field
126
     * @param mixed  $value
127
     * @param array  $selectedFields
128
     *
129
     * @return bool
130
     */
131
    protected function fieldShouldNotBeSaved($field, $value, $selectedFields)
132
    {
133
        return (!empty($selectedFields) && !in_array($field, $selectedFields)) || $field === 'ID';
134
    }
135
}
136