Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ActiveRecord often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ActiveRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | abstract class ActiveRecord extends Arrayy |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var DB static |
||
| 44 | */ |
||
| 45 | protected static $db; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array <p>Mapping the function name and the operator, to build Expressions in WHERE condition.</p> |
||
| 49 | * |
||
| 50 | * call the function like this: |
||
| 51 | * <pre> |
||
| 52 | * $user->isNotNull()->eq('id', 1); |
||
| 53 | * </pre> |
||
| 54 | * |
||
| 55 | * the result in SQL: |
||
| 56 | * <pre> |
||
| 57 | * WHERE user.id IS NOT NULL AND user.id = :ph1 |
||
| 58 | * </pre> |
||
| 59 | */ |
||
| 60 | protected static $operators = array( |
||
| 61 | 'equal' => '=', |
||
| 62 | 'eq' => '=', |
||
| 63 | 'notequal' => '<>', |
||
| 64 | 'ne' => '<>', |
||
| 65 | 'greaterthan' => '>', |
||
| 66 | 'gt' => '>', |
||
| 67 | 'lessthan' => '<', |
||
| 68 | 'lt' => '<', |
||
| 69 | 'greaterthanorequal' => '>=', |
||
| 70 | 'ge' => '>=', |
||
| 71 | 'gte' => '>=', |
||
| 72 | 'lessthanorequal' => '<=', |
||
| 73 | 'le' => '<=', |
||
| 74 | 'lte' => '<=', |
||
| 75 | 'between' => 'BETWEEN', |
||
| 76 | 'like' => 'LIKE', |
||
| 77 | 'in' => 'IN', |
||
| 78 | 'notin' => 'NOT IN', |
||
| 79 | 'isnull' => 'IS NULL', |
||
| 80 | 'isnotnull' => 'IS NOT NULL', |
||
| 81 | 'notnull' => 'IS NOT NULL', |
||
| 82 | ); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array <p>Part of the SQL, mapping the function name and the operator to build SQL Part.</p> |
||
| 86 | * |
||
| 87 | * <br /> |
||
| 88 | * |
||
| 89 | * call the function like this: |
||
| 90 | * <pre> |
||
| 91 | * $user->orderBy('id DESC', 'name ASC')->limit(2, 1); |
||
| 92 | * </pre> |
||
| 93 | * |
||
| 94 | * the result in SQL: |
||
| 95 | * <pre> |
||
| 96 | * ORDER BY id DESC, name ASC LIMIT 2,1 |
||
| 97 | * </pre> |
||
| 98 | */ |
||
| 99 | protected $sqlParts = array( |
||
| 100 | 'select' => 'SELECT', |
||
| 101 | 'from' => 'FROM', |
||
| 102 | 'set' => 'SET', |
||
| 103 | 'where' => 'WHERE', |
||
| 104 | 'group' => 'GROUP BY', |
||
| 105 | 'having' => 'HAVING', |
||
| 106 | 'order' => 'ORDER BY', |
||
| 107 | 'limit' => 'LIMIT', |
||
| 108 | 'top' => 'TOP', |
||
| 109 | ); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var array <p>The default sql expressions values.</p> |
||
| 113 | */ |
||
| 114 | protected $defaultSqlExpressions = array( |
||
| 115 | 'expressions' => array(), |
||
| 116 | 'wrap' => false, |
||
| 117 | 'select' => null, |
||
| 118 | 'insert' => null, |
||
| 119 | 'update' => null, |
||
| 120 | 'set' => null, |
||
| 121 | 'delete' => 'DELETE ', |
||
| 122 | 'join' => null, |
||
| 123 | 'from' => null, |
||
| 124 | 'values' => null, |
||
| 125 | 'where' => null, |
||
| 126 | 'having' => null, |
||
| 127 | 'limit' => null, |
||
| 128 | 'order' => null, |
||
| 129 | 'group' => null, |
||
| 130 | ); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array <p>Stored the Expressions of the SQL.</p> |
||
| 134 | */ |
||
| 135 | protected $sqlExpressions = array(); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var string <p>The table name in database.</p> |
||
| 139 | */ |
||
| 140 | protected $table; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var string <p>The primary key of this ActiveRecord, just support single primary key.</p> |
||
| 144 | */ |
||
| 145 | protected $primaryKeyName = 'id'; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var array <p>Stored the dirty data of this object, when call "insert" or "update" function, will write this data |
||
| 149 | * into database.</p> |
||
| 150 | */ |
||
| 151 | protected $dirty = array(); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var bool |
||
| 155 | */ |
||
| 156 | protected static $new_data_are_dirty = true; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var array <p>Stored the params will bind to SQL when call DB->query().</p> |
||
| 160 | */ |
||
| 161 | protected $params = array(); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var ActiveRecordExpressions[] <p>Stored the configure of the relation, or target of the relation.</p> |
||
| 165 | */ |
||
| 166 | protected $relations = array(); |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var int <p>The count of bind params, using this count and const "PREFIX" (:ph) to generate place holder in SQL.</p> |
||
| 170 | */ |
||
| 171 | private static $count = 0; |
||
| 172 | |||
| 173 | const BELONGS_TO = 'belongs_to'; |
||
| 174 | const HAS_MANY = 'has_many'; |
||
| 175 | const HAS_ONE = 'has_one'; |
||
| 176 | |||
| 177 | const PREFIX = ':active_record'; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | public function getParams() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getPrimaryKeyName() |
||
| 191 | { |
||
| 192 | return $this->primaryKeyName; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return mixed|null |
||
| 197 | */ |
||
| 198 | 13 | public function getPrimaryKey() |
|
| 199 | { |
||
| 200 | 13 | $id = $this->{$this->primaryKeyName}; |
|
| 201 | 13 | if ($id) { |
|
| 202 | 12 | return $id; |
|
| 203 | } |
||
| 204 | |||
| 205 | 1 | return null; |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param mixed $primaryKey |
||
| 210 | * @param bool $dirty |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | 1 | public function setPrimaryKey($primaryKey, $dirty = true) |
|
| 215 | { |
||
| 216 | 1 | if ($dirty === true) { |
|
| 217 | 1 | $this->dirty[$this->primaryKeyName] = $primaryKey; |
|
| 218 | 1 | } else { |
|
| 219 | $this->array[$this->primaryKeyName] = $primaryKey; |
||
| 220 | } |
||
| 221 | |||
| 222 | 1 | return $this; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function getTable() |
||
| 229 | { |
||
| 230 | return $this->table; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Function to reset the $params and $sqlExpressions. |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | 23 | public function reset() |
|
| 239 | { |
||
| 240 | 23 | $this->params = array(); |
|
| 241 | 23 | $this->sqlExpressions = array(); |
|
| 242 | |||
| 243 | 23 | return $this; |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Reset the dirty data. |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 6 | public function resetDirty() |
|
| 252 | { |
||
| 253 | 6 | $this->dirty = array(); |
|
| 254 | |||
| 255 | 6 | return $this; |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * set the DB connection. |
||
| 260 | * |
||
| 261 | * @param DB $db |
||
| 262 | */ |
||
| 263 | public static function setDb($db) |
||
| 264 | { |
||
| 265 | self::$db = $db; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Function to find one record and assign in to current object. |
||
| 270 | * |
||
| 271 | * @param mixed $id <p> |
||
| 272 | * If call this function using this param, we will find the record by using this id. |
||
| 273 | * If not set, just find the first record in database. |
||
| 274 | * </p> |
||
| 275 | * |
||
| 276 | * @return false|$this <p> |
||
| 277 | * If we could find the record, assign in to current object and return it, |
||
| 278 | * otherwise return "false". |
||
| 279 | * </p> |
||
| 280 | */ |
||
| 281 | 11 | View Code Duplication | public function fetch($id = null) |
|
|
|||
| 282 | { |
||
| 283 | 11 | if ($id) { |
|
| 284 | 6 | $this->reset()->eq($this->primaryKeyName, $id); |
|
| 285 | 6 | } |
|
| 286 | |||
| 287 | 11 | return self::query( |
|
| 288 | 11 | $this->limit(1)->_buildSql( |
|
| 289 | array( |
||
| 290 | 11 | 'select', |
|
| 291 | 11 | 'from', |
|
| 292 | 11 | 'join', |
|
| 293 | 11 | 'where', |
|
| 294 | 11 | 'group', |
|
| 295 | 11 | 'having', |
|
| 296 | 11 | 'order', |
|
| 297 | 11 | 'limit', |
|
| 298 | ) |
||
| 299 | 11 | ), |
|
| 300 | 11 | $this->params, |
|
| 301 | 11 | $this->reset(), |
|
| 302 | true |
||
| 303 | 11 | ); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $query |
||
| 308 | * |
||
| 309 | * @return $this[] |
||
| 310 | */ |
||
| 311 | 1 | public function fetchManyByQuery($query) |
|
| 312 | { |
||
| 313 | 1 | $list = $this->fetchByQuery($query); |
|
| 314 | |||
| 315 | 1 | if (!$list || empty($list)) { |
|
| 316 | return array(); |
||
| 317 | } |
||
| 318 | |||
| 319 | 1 | return $list; |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $query |
||
| 324 | * |
||
| 325 | * @return $this|null |
||
| 326 | */ |
||
| 327 | 1 | public function fetchOneByQuery($query) |
|
| 328 | { |
||
| 329 | 1 | $list = $this->fetchByQuery($query); |
|
| 330 | |||
| 331 | 1 | if (!$list || empty($list)) { |
|
| 332 | return null; |
||
| 333 | } |
||
| 334 | |||
| 335 | 1 | if (is_array($list) && count($list) > 0) { |
|
| 336 | 1 | $this->array = $list[0]->getArray(); |
|
| 337 | 1 | } else { |
|
| 338 | $this->array = $list->getArray(); |
||
| 339 | } |
||
| 340 | |||
| 341 | 1 | return $this; |
|
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param mixed $id |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | * |
||
| 349 | * @throws FetchingException <p>Will be thrown, if we can not find the id.</p> |
||
| 350 | */ |
||
| 351 | 2 | public function fetchById($id) |
|
| 352 | { |
||
| 353 | 2 | $obj = $this->fetchByIdIfExists($id); |
|
| 354 | 2 | if ($obj === null) { |
|
| 355 | 1 | throw new FetchingException("No row with primary key '$id' in table '$this->table'."); |
|
| 356 | } |
||
| 357 | |||
| 358 | 1 | return $obj; |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param mixed $id |
||
| 363 | * |
||
| 364 | * @return $this|null |
||
| 365 | */ |
||
| 366 | 4 | public function fetchByIdIfExists($id) |
|
| 367 | { |
||
| 368 | 4 | $list = $this->fetch($id); |
|
| 369 | |||
| 370 | 4 | if (!$list || $list->isEmpty()) { |
|
| 371 | 2 | return null; |
|
| 372 | } |
||
| 373 | |||
| 374 | 2 | return $list; |
|
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param array $ids |
||
| 379 | * |
||
| 380 | * @return $this[] |
||
| 381 | */ |
||
| 382 | 2 | public function fetchByIds(array $ids) |
|
| 383 | { |
||
| 384 | 2 | if (empty($ids)) { |
|
| 385 | return array(); |
||
| 386 | } |
||
| 387 | |||
| 388 | 2 | $list = $this->fetchAll($ids); |
|
| 389 | 2 | if (is_array($list) && count($list) > 0) { |
|
| 390 | 1 | return $list; |
|
| 391 | } |
||
| 392 | |||
| 393 | 1 | return array(); |
|
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $query |
||
| 398 | * |
||
| 399 | * @return $this[]|$this |
||
| 400 | */ |
||
| 401 | 2 | public function fetchByQuery($query) |
|
| 402 | { |
||
| 403 | 2 | $list = self::query( |
|
| 404 | 2 | $query, |
|
| 405 | 2 | $this->params, |
|
| 406 | 2 | $this->reset() |
|
| 407 | 2 | ); |
|
| 408 | |||
| 409 | 2 | if (is_array($list)) { |
|
| 410 | 2 | if (count($list) === 0) { |
|
| 411 | return array(); |
||
| 412 | } |
||
| 413 | |||
| 414 | 2 | return $list; |
|
| 415 | } |
||
| 416 | |||
| 417 | $this->array = $list->getArray(); |
||
| 418 | |||
| 419 | return $this; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param array $ids |
||
| 424 | * |
||
| 425 | * @return $this[] |
||
| 426 | */ |
||
| 427 | 1 | public function fetchByIdsPrimaryKeyAsArrayIndex(array $ids) |
|
| 428 | { |
||
| 429 | 1 | $result = $this->fetchAll($ids); |
|
| 430 | |||
| 431 | 1 | $resultNew = array(); |
|
| 432 | 1 | foreach ($result as $item) { |
|
| 433 | 1 | $resultNew[$item->getPrimaryKey()] = $item; |
|
| 434 | 1 | } |
|
| 435 | |||
| 436 | 1 | return $resultNew; |
|
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Function to find all records in database. |
||
| 441 | * |
||
| 442 | * @param array|null $ids <p> |
||
| 443 | * If call this function using this param, we will find the record by using this id's. |
||
| 444 | * If not set, just find all records in database. |
||
| 445 | * </p> |
||
| 446 | * |
||
| 447 | * @return $this[] |
||
| 448 | */ |
||
| 449 | 6 | View Code Duplication | public function fetchAll(array $ids = null) |
| 450 | { |
||
| 451 | 6 | if ($ids) { |
|
| 452 | 3 | $this->reset()->in($this->primaryKeyName, $ids); |
|
| 453 | 3 | } |
|
| 454 | |||
| 455 | 6 | return self::query( |
|
| 456 | 6 | $this->_buildSql( |
|
| 457 | array( |
||
| 458 | 6 | 'select', |
|
| 459 | 6 | 'from', |
|
| 460 | 6 | 'join', |
|
| 461 | 6 | 'where', |
|
| 462 | 6 | 'groupBy', |
|
| 463 | 6 | 'having', |
|
| 464 | 6 | 'orderBy', |
|
| 465 | 6 | 'limit', |
|
| 466 | ) |
||
| 467 | 6 | ), |
|
| 468 | 6 | $this->params, |
|
| 469 | 6 | $this->reset() |
|
| 470 | 6 | ); |
|
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Function to delete current record in database. |
||
| 475 | * |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | 1 | public function delete() |
|
| 479 | { |
||
| 480 | 1 | return self::execute( |
|
| 481 | 1 | $this->eq($this->primaryKeyName, $this->{$this->primaryKeyName})->_buildSql( |
|
| 482 | array( |
||
| 483 | 1 | 'delete', |
|
| 484 | 1 | 'from', |
|
| 485 | 1 | 'where', |
|
| 486 | ) |
||
| 487 | 1 | ), |
|
| 488 | 1 | $this->params |
|
| 489 | 1 | ); |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param string $primaryKeyName |
||
| 494 | * |
||
| 495 | * @return $this |
||
| 496 | */ |
||
| 497 | public function setPrimaryKeyName($primaryKeyName) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $table |
||
| 506 | */ |
||
| 507 | public function setTable($table) |
||
| 508 | { |
||
| 509 | $this->table = $table; |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Function to build update SQL, and update current record in database, just write the dirty data into database. |
||
| 514 | * |
||
| 515 | * @return bool|int <p> |
||
| 516 | * If update was successful, it will return the affected rows as int, |
||
| 517 | * otherwise it will return false or true (if there are no dirty data). |
||
| 518 | * </p> |
||
| 519 | */ |
||
| 520 | 2 | public function update() |
|
| 521 | { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @return $this |
||
| 552 | */ |
||
| 553 | 1 | public static function fetchEmpty() |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Function to build insert SQL, and insert current record into database. |
||
| 561 | * |
||
| 562 | * @return bool|int <p> |
||
| 563 | * If insert was successful, it will return the new id, |
||
| 564 | * otherwise it will return false or true (if there are no dirty data). |
||
| 565 | * </p> |
||
| 566 | */ |
||
| 567 | 4 | public function insert() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Helper function to copy an existing active record (and insert it into the database). |
||
| 606 | * |
||
| 607 | * @param bool $insert |
||
| 608 | * |
||
| 609 | * @return $this |
||
| 610 | */ |
||
| 611 | 2 | public function copy($insert = true) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Helper function to exec sql. |
||
| 626 | * |
||
| 627 | * @param string $sql <p>The SQL need to be execute.</p> |
||
| 628 | * @param array $param <p>The param will be bind to the sql statement.</p> |
||
| 629 | * |
||
| 630 | * @return bool|int|Result <p> |
||
| 631 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 632 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 633 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 634 | * "true" by e.g. "DROP"-queries<br /> |
||
| 635 | * "false" on error |
||
| 636 | * </p> |
||
| 637 | */ |
||
| 638 | 24 | public static function execute($sql, array $param = array()) |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Helper function to query one record by sql and params. |
||
| 649 | * |
||
| 650 | * @param string $sql <p> |
||
| 651 | * The SQL query to find the record. |
||
| 652 | * </p> |
||
| 653 | * @param array $param <p> |
||
| 654 | * The param will be bind to the $sql query. |
||
| 655 | * </p> |
||
| 656 | * @param ActiveRecord|null $obj <p> |
||
| 657 | * The object, if find record in database, we will assign the attributes into |
||
| 658 | * this object. |
||
| 659 | * </p> |
||
| 660 | * @param bool $single <p> |
||
| 661 | * If set to true, we will find record and fetch in current object, otherwise |
||
| 662 | * will find all records. |
||
| 663 | * </p> |
||
| 664 | * |
||
| 665 | * @return bool|$this|array |
||
| 666 | */ |
||
| 667 | 17 | public static function query($sql, array $param = array(), ActiveRecord $obj = null, $single = false) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Helper function to get relation of this object. |
||
| 697 | * There was three types of relations: {BELONGS_TO, HAS_ONE, HAS_MANY} |
||
| 698 | * |
||
| 699 | * @param string $name <p>The name of the relation (the array key from the definition).</p> |
||
| 700 | * |
||
| 701 | * @return mixed |
||
| 702 | * |
||
| 703 | * @throws ActiveRecordException <p>If the relation can't be found .</p> |
||
| 704 | */ |
||
| 705 | 3 | protected function &getRelation($name) |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Helper function to build SQL with sql parts. |
||
| 778 | * |
||
| 779 | * @param string $n <p>The SQL part will be build.</p> |
||
| 780 | * @param int $i <p>The index of $n in $sql array.</p> |
||
| 781 | * @param ActiveRecord $o <p>The reference to $this.</p> |
||
| 782 | */ |
||
| 783 | 21 | private function _buildSqlCallback(&$n, $i, $o) |
|
| 815 | |||
| 816 | /** |
||
| 817 | * Helper function to build SQL with sql parts. |
||
| 818 | * |
||
| 819 | * @param array $sqls <p>The SQL part will be build.</p> |
||
| 820 | * |
||
| 821 | * @return string |
||
| 822 | */ |
||
| 823 | 21 | protected function _buildSql($sqls = array()) |
|
| 832 | |||
| 833 | /** |
||
| 834 | * Magic function to make calls witch in function mapping stored in $operators and $sqlPart. |
||
| 835 | * also can call function of DB object. |
||
| 836 | * |
||
| 837 | * @param string $name <p>The name of the function.</p> |
||
| 838 | * @param array $args <p>The arguments of the function.</p> |
||
| 839 | * |
||
| 840 | * @return $this|mixed <p>Return the result of callback or the current object to make chain method calls.</p> |
||
| 841 | * |
||
| 842 | * @throws ActiveRecordException |
||
| 843 | */ |
||
| 844 | 16 | public function __call($name, $args) |
|
| 882 | |||
| 883 | /** |
||
| 884 | * Make wrap when build the SQL expressions of WHERE. |
||
| 885 | * |
||
| 886 | * @param string $op <p>If given, this param will build one "ActiveRecordExpressionsWrap" and include the stored expressions add into WHERE, |
||
| 887 | * otherwise it will stored the expressions into an array.</p> |
||
| 888 | * |
||
| 889 | * @return $this |
||
| 890 | */ |
||
| 891 | 1 | public function wrap($op = null) |
|
| 912 | |||
| 913 | /** |
||
| 914 | * Helper function to build place holder when make SQL expressions. |
||
| 915 | * |
||
| 916 | * @param mixed $value <p>The value will be bind to SQL, just store it in $this->params.</p> |
||
| 917 | * |
||
| 918 | * @return mixed $value |
||
| 919 | */ |
||
| 920 | 18 | protected function _filterParam($value) |
|
| 933 | |||
| 934 | /** |
||
| 935 | * Helper function to add condition into WHERE. |
||
| 936 | * |
||
| 937 | * @param string $field <p>The field name, the source of Expressions</p> |
||
| 938 | * @param string $operator |
||
| 939 | * @param mixed $value <p>The target of the Expressions.</p> |
||
| 940 | * @param string $op <p>The operator to concat this Expressions into WHERE or SET statement.</p> |
||
| 941 | * @param string $name <p>The Expression will contact to.</p> |
||
| 942 | */ |
||
| 943 | 14 | public function addCondition($field, $operator, $value, $op = 'AND', $name = 'where') |
|
| 966 | |||
| 967 | /** |
||
| 968 | * Helper function to add condition into JOIN. |
||
| 969 | * |
||
| 970 | * @param string $table <p>The join table name.</p> |
||
| 971 | * @param string $on <p>The condition of ON.</p> |
||
| 972 | * @param string $type <p>The join type, like "LEFT", "INNER", "OUTER".</p> |
||
| 973 | * |
||
| 974 | * @return $this |
||
| 975 | */ |
||
| 976 | 1 | public function join($table, $on, $type = 'LEFT') |
|
| 990 | |||
| 991 | /** |
||
| 992 | * helper function to make wrapper. Stored the expression in to array. |
||
| 993 | * |
||
| 994 | * @param ActiveRecordExpressions $exp <p>The expression will be stored.</p> |
||
| 995 | * @param string $operator <p>The operator to concat this Expressions into WHERE statement.</p> |
||
| 996 | */ |
||
| 997 | 1 | protected function _addExpression($exp, $operator) |
|
| 1009 | |||
| 1010 | /** |
||
| 1011 | * helper function to add condition into WHERE. |
||
| 1012 | * |
||
| 1013 | * @param ActiveRecordExpressions $exp <p>The expression will be concat into WHERE or SET statement.</p> |
||
| 1014 | * @param string $operator <p>The operator to concat this Expressions into WHERE or SET statement.</p> |
||
| 1015 | * @param string $name <p>The Expression will contact to.</p> |
||
| 1016 | */ |
||
| 1017 | 14 | protected function _addCondition($exp, $operator, $name = 'where') |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * @return array |
||
| 1034 | */ |
||
| 1035 | 1 | public function getDirty() |
|
| 1039 | |||
| 1040 | /** |
||
| 1041 | * @return bool |
||
| 1042 | */ |
||
| 1043 | public static function isNewDataAreDirty() |
||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * @param bool $bool |
||
| 1050 | */ |
||
| 1051 | 17 | public static function setNewDataAreDirty($bool) |
|
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Magic function to SET values of the current object. |
||
| 1058 | * |
||
| 1059 | * @param mixed $var |
||
| 1060 | * @param mixed $val |
||
| 1061 | */ |
||
| 1062 | 23 | public function __set($var, $val) |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Magic function to UNSET values of the current object. |
||
| 1093 | * |
||
| 1094 | * @param mixed $var |
||
| 1095 | */ |
||
| 1096 | 1 | public function __unset($var) |
|
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Helper function for "GROUP BY". |
||
| 1113 | * |
||
| 1114 | * @param array $args |
||
| 1115 | * |
||
| 1116 | * @return $this |
||
| 1117 | */ |
||
| 1118 | public function groupBy($args) |
||
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Helper function for "ORDER BY". |
||
| 1127 | * |
||
| 1128 | * @param $args ... |
||
| 1129 | * |
||
| 1130 | * @return $this |
||
| 1131 | */ |
||
| 1132 | 2 | public function orderBy($args) |
|
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Magic function to GET the values of current object. |
||
| 1141 | * |
||
| 1142 | * @param $var |
||
| 1143 | * |
||
| 1144 | * @return mixed |
||
| 1145 | */ |
||
| 1146 | 23 | public function &__get($var) |
|
| 1162 | } |
||
| 1163 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.