PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/surosql/SurosqlOO.class.php

https://bitbucket.org/SuRaMoN/surosql
PHP | 65 lines | 50 code | 14 blank | 1 comment | 6 complexity | 47c8166a96e49366896f8b8e2007177b MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. // version: 3.1.0
  3. namespace ns;
  4. class SurosqlOO {
  5. const ONLY_COLUMN_FIELDS = 0;
  6. const ALSO_NON_COLUMN_FIELDS = 1;
  7. function __construct($field_array = null) {
  8. if(is_array($field_array)) {
  9. $this->set_fields($field_array);
  10. }
  11. }
  12. function set_fields($field_array, $which_fields = self::ONLY_COLUMN_FIELDS) {
  13. if($which_fields == self::ONLY_COLUMN_FIELDS) {
  14. $oo = SurosqlOOManager::get_osql_inst($this)->get_obj_info($this);
  15. $field_array = array_intersect_key($field_array, array_flip($oo['columns']));
  16. }
  17. foreach($field_array as $name => $value) {
  18. $this->$name = $value;
  19. }
  20. return $this;
  21. }
  22. function update($fields = null, $values_unescaped = array()) {
  23. return SurosqlOOManager::update($this, $fields, $values_unescaped);
  24. }
  25. function insert($fields = null, $values_unescaped = array()) {
  26. return SurosqlOOManager::insert($this, $fields, $values_unescaped);
  27. }
  28. function replace($fields = null, $values_unescaped = array()) {
  29. return SurosqlOOManager::replace($this, $fields, $values_unescaped);
  30. }
  31. function insertorupdate($fields = null, $values_unescaped = array()) {
  32. return SurosqlOOManager::insertorupdate($this, $fields, $values_unescaped);
  33. }
  34. function delete() {
  35. return SurosqlOOManager::delete($this);
  36. }
  37. static function get($criteria = null) {
  38. $osql = SurosqlOOManager::get_osql_inst(get_called_class());
  39. $info = $osql->get_class_info(get_called_class());
  40. $query = $osql->fromoo($info['alias']);
  41. if(is_array($criteria)) {
  42. foreach($criteria as $field => $value) {
  43. $query->ifeq($field, $value);
  44. }
  45. } else if(!is_null($criteria)) {
  46. $query->ifeq($info['primary'], $criteria);
  47. }
  48. return $query;
  49. }
  50. }
  51. ?>