PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / 1.2.10
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel v1.2.10
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / models / model / record.php
wp-all-export / models / model Last commit date
list.php 4 years ago record.php 4 years ago
record.php
176 lines
1 <?php
2 /**
3 * Base class for models
4 *
5 * @author Pavel Kulbakin <p.kulbakin@gmail.com>
6 */
7 class PMXE_Model_Record extends PMXE_Model {
8 /**
9 * Initialize model
10 * @param array[optional] $data Array of record data to initialize object with
11 */
12 public function __construct($data = array()) {
13 parent::__construct();
14 if (! is_array($data)) {
15 throw new Exception("Array expected as paramenter for " . get_class($this) . "::" . __METHOD__);
16 }
17 $data and $this->set($data);
18 }
19
20 /**
21 * @see PMXE_Model::getBy()
22 * @return PMXE_Model_Record
23 */
24 public function getBy($field = NULL, $value = NULL) {
25 if (is_null($field)) {
26 throw new Exception("Field parameter is expected at " . get_class($this) . "::" . __METHOD__);
27 }
28 $sql = "SELECT * FROM $this->table WHERE " . $this->buildWhere($field, $value);
29 $result = $this->wpdb->get_row($sql, ARRAY_A);
30 if (is_array($result)) {
31 foreach ($result as $k => $v) {
32 if (is_serialized($v)) {
33 $result[$k] = unserialize($v);
34 }
35 }
36 $this->exchangeArray($result);
37 } else {
38 $this->clear();
39 }
40 return $this;
41 }
42
43 /**
44 * Ger records related to current one
45 * @param string $model Class name of model of related records
46 * @param array[optoinal] $keyAssoc
47 * @return PMXE_Model_List
48 */
49 public function getRelated($model, $keyAssoc = NULL) {
50 $related = new $model();
51 if ( ! empty($this->id)) {
52 if (is_null($keyAssoc)) {
53 $defaultPrefix = strtolower(preg_replace('%^' . strtoupper(PMXE_Plugin::PREFIX) . '|_Record$%', '', get_class($this)));
54 $keyAssoc = array();
55 foreach ($this->primary as $key) {
56 $keyAssoc = array($defaultPrefix . '_' . $key => $key);
57 }
58 }
59 foreach ($keyAssoc as $foreign => $local) {
60 $keyAssoc[$foreign] = $this->$local;
61 }
62 $related->getBy($keyAssoc);
63 }
64 return $related instanceof PMXE_Model_List ? $related->convertRecords() : $related;
65 }
66
67 /**
68 * Saves currently set object data as database record
69 * @return PMXE_Model_Record
70 */
71 public function insert() {
72 if ($this->wpdb->insert($this->table, $this->toArray(TRUE))) {
73 if (isset($this->auto_increment)) {
74 $this[$this->primary[0]] = $this->wpdb->insert_id;
75 }
76 return $this;
77 } else {
78 throw new Exception($this->wpdb->last_error);
79 }
80 }
81 /**
82 * Update record in database
83 * @return PMXE_Model_Record
84 */
85 public function update() {
86 $record = $this->toArray(TRUE);
87 $this->wpdb->update($this->table, $record, array_intersect_key($record, array_flip($this->primary)));
88 if ($this->wpdb->last_error) {
89 throw new Exception($this->wpdb->last_error);
90 }
91 return $this;
92 }
93
94 /**
95 * Delete record form database
96 * @return PMXE_Model_Record
97 */
98 public function delete() {
99 if ($this->wpdb->query("DELETE FROM $this->table WHERE " . $this->buildWhere(array_intersect_key($this->toArray(TRUE), array_flip($this->primary))))) {
100 return $this;
101 } elseif ($this->wpdb->last_error) {
102 throw new Exception($this->wpdb->last_error);
103 }
104 }
105 /**
106 * Insert or Update the record
107 * WARNING: function doesn't check actual record presents in database, it simply tries to insert if no primary key specified and update otherwise
108 * @return PMXE_Model_Record
109 */
110 public function save() {
111 if (array_intersect_key($this->toArray(TRUE), array_flip($this->primary))) {
112 $this->update();
113 } else {
114 $this->insert();
115 }
116 return $this;
117 }
118
119 /**
120 * Set record data
121 * When 1st parameter is an array, it expected to be an associative array of field => value pairs
122 * If 2 parameters are set, first one is expected to be a field name and second - it's value
123 *
124 * @param string|array $field
125 * @param mixed[optional] $value
126 * @return PMXE_Model_Record
127 */
128 public function set($field, $value = NULL) {
129 if (is_array($field) and ( ! is_null($value) or 0 == count($field))) {
130 throw new Exception(__CLASS__ . "::set method expects either not empty associative array as the only paramter or field name and it's value as two seperate parameters.");
131 }
132 if (is_array($field)) {
133 $this->exchangeArray(array_merge($this->toArray(), $field));
134 } else {
135 $this[$field] = $value;
136 }
137
138 return $this;
139 }
140
141 /**
142 * Magic method to resolved object-like request to record values in format $obj->%FIELD_NAME%
143 * @param string $field
144 * @return mixed
145 */
146 public function __get($field) {
147 if ( ! $this->offsetExists($field)) {
148 throw new Exception("Undefined field $field.");
149 }
150 return $this[$field];
151 }
152 /**
153 * Magic method to assign values to record fields in format $obj->%FIELD_NAME = value
154 * @param string $field
155 * @param mixed $value
156 */
157 public function __set($field, $value) {
158 $this[$field] = $value;
159 }
160 /**
161 * Magic method to check wether some record fields are set
162 * @param string $field
163 * @return bool
164 */
165 public function __isset($field) {
166 return $this->offsetExists($field);
167 }
168 /**
169 * Magic method to unset record fields
170 * @param string $field
171 */
172 public function __unset($field) {
173 $this->offsetUnset($field);
174 }
175
176 }