/
Resource.php
86 lines (73 loc) · 2.47 KB
/
Resource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
class OnlineShop_Framework_Impl_CartCheckoutData_Resource extends \Pimcore\Model\Resource\AbstractResource {
const TABLE_NAME = "plugin_onlineshop_cartcheckoutdata";
/**
* Contains all valid columns in the database table
*
* @var array
*/
protected $validColumns = array();
protected $fieldsToSave = array("cartId", "key", "data");
/**
* Get the valid columns from the database
*
* @return void
*/
public function init() {
$this->validColumns = $this->getValidTableColumns(self::TABLE_NAME);
}
/**
* @throws Exception
* @param string $key
* @param int $cartId
* @return void
*/
public function getByKeyCartId($key, $cartId) {
$classRaw = $this->db->fetchRow("SELECT * FROM " . self::TABLE_NAME . " WHERE `key`=" . $this->db->quote($key). " AND cartId = " . $this->db->quote($cartId));
if(empty($classRaw)) {
throw new Exception("CartItem for cartid " . $cartId . " and key " . $key . " not found.");
}
$this->assignVariablesToModel($classRaw);
}
/**
* Save object to database
*
* @return void
*/
public function save() {
return $this->update();
}
/**
* @return void
*/
public function update() {
foreach ($this->fieldsToSave as $field) {
if (in_array($field, $this->validColumns)) {
$getter = "get" . ucfirst($field);
$value = $this->model->$getter();
if (is_array($value) || is_object($value)) {
$value = serialize($value);
} else if(is_bool($value)) {
$value = (int)$value;
}
$data[$field] = $value;
}
}
try {
$this->db->insert(self::TABLE_NAME, $data);
} catch(Exception $e) {
$this->db->update(self::TABLE_NAME, $data, "`key`=" . $this->db->quote($this->model->getKey()). " AND cartId = " . $this->db->quote($this->model->getCartId()));
}
}
/**
* Deletes object from database
*
* @return void
*/
public function delete() {
$this->db->delete(self::TABLE_NAME, "productId=" . $this->db->quote($this->model->productId). " AND cartId = " . $this->db->quote($this->model->cartId));
}
public function removeAllFromCart($cartId) {
$this->db->delete(self::TABLE_NAME, "cartId = " . $this->db->quote($cartId));
}
}