Model
Source
File: Model/Model.php
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | abstract class Model { public $timestamp = true; protected $columns = []; /** * The table associated with the model. * * @var string */ protected $table ; /** * The primary key for the model. * * @var string */ protected $primaryKey = 'ID' ; /** * The number of models to return for pagination. * * @var int */ protected $per_page ; public function __construct( $model_id = 0 ) { global $wpdb ; $this ->table = $wpdb ->prefix . $this ->table; if ( ! empty ( $model_id ) ) { $model_raw = $wpdb ->get_row( " SELECT * FROM {$this->table} WHERE {$this->primaryKey} = {$model_id} " , ARRAY_A ); if ( $model_raw ) { $this ->syncModel( $model_raw ); } } } /** * Save the model to the database. * * @param array $options * * @return bool|mixed */ public function save( array $options = [] ) { global $wpdb ; $columns = $this ->db_columns( $options ); $is_insert = empty ( $columns [ $this ->primaryKey ] ); if ( $is_insert ) { if ( $this ->timestamp ) { $columns [ 'created_at' ] = dozent_mysql_time(); } $wpdb ->insert( $this ->table, $columns ); $insert_id = (int) $wpdb ->insert_id; } else { $updated_columns = dozent_array_only( get_object_vars( $this ), array_keys ( $columns ) ); unset( $updated_columns [ $this ->primaryKey ] ); $wpdb ->update( $this ->table, $updated_columns , [ $this ->primaryKey => $columns [ $this ->primaryKey ] ] ); $insert_id = (int) $columns [ $this ->primaryKey ]; $columns = array_merge ( $columns , $updated_columns ); } if ( $insert_id ) { $columns [ $this ->primaryKey ] = $insert_id ; $this ->syncModel( $columns ); return $this ; } return new \WP_Error( 'dozent_model_saving_error' , __( ' Something went wrong ' , 'dozent' ) ); } /** * Modal valid database column * * * @since DozentLMS 1.0.0 * * * @param $options * * @return array Database Column */ public function db_columns( $options ) { $columns = array_merge ( get_object_vars( $this ), $this ->columns ); /** * Filter default model properties as DB column which not required or not available in the actual DB table * * @since DozentLMS 1.0.0 */ $db_column_except = apply_filters( 'dozent_db_columns_except' , [ 'table' , 'timestamp' , 'columns' , 'primaryKey' , 'per_page' ] ); $columns = dozent_array_except( $columns , $db_column_except ); if ( dozent_count( $options ) ) { $columns = $options ; } $columns = array_filter ( $columns ); return $columns ; } protected function syncModel( $columns = [] ) { $this ->columns = array_merge ( $this ->columns, $columns ); $columns = $this ->columns; if ( ! empty ( $columns [ $this ->primaryKey ] ) ) { $primary_key = $columns [ $this ->primaryKey ]; unset( $columns [ $this ->primaryKey ] ); $columns = array_merge ( [ $this ->primaryKey => $primary_key ], $columns ); } foreach ( $columns as $property => $value ) { $this ->{ $property } = $value ; } $this ->columns = $columns ; } /** * Dynamically retrieve attributes on the model. * * @param string $key * * @return mixed */ public function __get( $key ) { return $this ->getAttribute( $key ); } public function getAttribute( $key ) { $value = dozent_array_get( $key , $this ->columns ); return $value ; } /** * Dynamically set attributes on the model. * * @param string $key * @param mixed $value * * @return void */ public function __set( $key , $value ) { $this ->columns[ $key ] = $value ; } /** * Convert the model to its string representation. * * @return string */ public function __toString() { return $this ->toJson(); } /** * Convert the model to array * * * @return array */ public function toArray() { return ( array ) $this ; } public function toJson( $options = 0 ) { $json = json_encode( $this ->toArray(), $options ); if ( JSON_ERROR_NONE !== json_last_error() ) { throw new \Exception( 'Error encoding model [' . get_class( $this ) . '] to JSON: ' . json_last_error_msg() ); } return $json ; } } |
Methods
- __construct
- __get — Dynamically retrieve attributes on the model.
- __set — Dynamically set attributes on the model.
- __toString — Convert the model to its string representation.
- db_columns — Modal valid database column
- getAttribute
- save — Save the model to the database.
- syncModel
- toArray — Convert the model to array
- toJson