bpModAbstractDBobj

Abstract DB Object.

Defined (1)

The class is defined in the following location(s).

/classes/bpModAbstractDBobj.php  
  1. class bpModAbstractDBobj 
  2.  
  3. /** 
  4. * database table name 
  5. * @var string 
  6. */ 
  7. var $__table; 
  8.  
  9. /** 
  10. * name of the object (used for calling actions and filters) 
  11. * @var string 
  12. */ 
  13. var $__obj_name; 
  14.  
  15. /** 
  16. * primary key column name 
  17. * @var string 
  18. */ 
  19. var $__id_field; 
  20.  
  21. /** 
  22. * associative array that relates columns name to the corresponding format 
  23. * 'column_name' => '%s' or '%d' 
  24. * primary key is considered %d by default 
  25. * if a format is not defined %s is used 
  26. * @var array 
  27. */ 
  28. var $__fields_format = array(); 
  29.  
  30. /** 
  31. * stores value of primary key 
  32. * @var int 
  33. */ 
  34. var $__id; 
  35.  
  36. /** 
  37. * associative arrays that stores values of columns other than primary key 
  38. * 'column_name' => $value 
  39. * @var array 
  40. */ 
  41. var $__data = array(); 
  42.  
  43. function __construct($id = false) 
  44. if ((int)$id) { 
  45. $this->populate($id); 
  46.  
  47. /** 
  48. * populate() 
  49. * This method will populate the object with a row from the database, based on the 
  50. * ID passed to the constructor. 
  51. */ 
  52. function populate($id) 
  53. global $wpdb; 
  54.  
  55. $sql = $wpdb->prepare("SELECT * FROM {$this->__table} WHERE {$this->__id_field} = %d", (int)$id); 
  56. $row = $wpdb->get_row($sql, ARRAY_A); 
  57. if ($row && !empty($row[$this->__id_field])) { 
  58. $this->__id = $row[$this->__id_field]; 
  59. unset($row[$this->__id_field]); 
  60. $this->__data = $row; 
  61.  
  62. /** 
  63. * save() 
  64. * This method will save an object to the database. It will dynamically switch between 
  65. * INSERT and UPDATE depending on whether or not the object already exists in the database. 
  66. */ 
  67. function save() 
  68. global $wpdb; 
  69.  
  70. $formats = array(); 
  71.  
  72. //note: $value can't be referenced in php4 
  73. foreach ($this->__data as $field => $value) { 
  74.  
  75. $this->__data[$field] = apply_filters($this->__obj_name . '_data_' . $field . '_before_save', $value, $this->__id); 
  76.  
  77. if (!empty($this->__fields_format[$field])) { 
  78. $formats[] = $this->__fields_format[$field]; 
  79. else 
  80. $formats[] = '%s'; 
  81.  
  82. /** Call a before save action here */ 
  83. do_action($this->__obj_name . '_data_before_save', array(&$this)); 
  84.  
  85. if ($this->__id) { 
  86. // Update 
  87. $result = $wpdb->update($this->__table, $this->__data, array($this->__id_field => $this->__id), $formats, array('%d')); 
  88. } else { 
  89. // Save 
  90. $result = $wpdb->insert($this->__table, $this->__data, $formats); 
  91.  
  92. if (!$result) { 
  93. return false; 
  94.  
  95. if (!$this->__id) { 
  96. $this->__id = $wpdb->insert_id; 
  97.  
  98. /** Add an after save action here */ 
  99. do_action($this->__obj_name . '_data_after_save', array(&$this)); 
  100.  
  101. return $result; 
  102.  
  103. /** 
  104. * delete() 
  105. * This method will delete the corresponding row for an object from the database. 
  106. * @return bool if the row has been deleted 
  107. */ 
  108. function delete() 
  109. global $wpdb; 
  110.  
  111. return $wpdb->query($wpdb->prepare("DELETE FROM {$this->__table} WHERE {$this->__id_field} = %d", $this->__id)); 
  112.  
  113. /** 
  114. * give a more covenient way of storing fields value without have to remember 
  115. * where they are stored 
  116. * @param string $field | field to store in 
  117. * @param mixed $vaule | value to store 
  118. */ 
  119. function __set($field, $value) 
  120. if ($field == $this->__id_field) { 
  121. $this->__id = $value; 
  122. else 
  123. $this->__data[$field] = $value; 
  124.  
  125. /** 
  126. * give a more covenient way of getting fields value without have to remember 
  127. * where they are stored 
  128. * @param string $field | field to get 
  129. * @return mixed $value 
  130. */ 
  131. function __get($field) 
  132. if ($field == $this->__id_field) { 
  133. return $this->__id; 
  134. elseif (isset ($this->__data[$field])) 
  135. return $this->__data[$field]; 
  136. else 
  137. return null;