/[drupal]/contributions/modules/object_driver/object_driver.module
ViewVC logotype

Diff of /contributions/modules/object_driver/object_driver.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.9, Sun Apr 8 16:45:03 2007 UTC revision 1.10, Thu Sep 13 01:59:21 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: object_driver.module,v 1.8 2007/04/08 16:23:32 pwolanin Exp $  // $Id: object_driver.module,v 1.9 2007/04/08 16:45:03 pwolanin Exp $
3    
4  /**  /**
5   * hooks this module looks for (hook -> module name, classhook -> class name):   * hooks this module looks for (hook -> module name, classhook -> class name):
# Line 459  function object_driver_load($class, $id, Line 459  function object_driver_load($class, $id,
459      $obj_def = call_user_func($class .'_class_define');      $obj_def = call_user_func($class .'_class_define');
460      $table = isset($obj_def['table']) ? $obj_def['table'] : $class;      $table = isset($obj_def['table']) ? $obj_def['table'] : $class;
461    
462      $obj = db_fetch_object(db_query("SELECT * FROM {%s} WHERE %s = %d", $table, $id_name, $id));      $obj = db_fetch_object(db_query("SELECT * FROM {". $table ."} WHERE %s = %d", $id_name, $id));
463    
464    }    }
465    if (isset($obj->$id_name)) {    if (isset($obj->$id_name)) {
# Line 478  function object_driver_load($class, $id, Line 478  function object_driver_load($class, $id,
478    
479  function object_driver_save(&$obj) {  function object_driver_save(&$obj) {
480    $class = $obj->class;    $class = $obj->class;
   
481    $id_name = $class .'_id';    $id_name = $class .'_id';
482    $obj->is_new = FALSE;    // Is this a new object?
483      $obj->is_new = empty($obj->$id_name);
484    
   if (empty($obj->$id_name)) {  
     // Insert a new object  
     $obj->is_new = TRUE;  
     $obj->$id_name = db_next_id("{$table}_id");  
   }  
   
485    if (function_exists($class .'_save')) {    if (function_exists($class .'_save')) {
486      call_user_func($class .'_save', $obj);      call_user_func($class .'_save', $obj);
487    }    }
488    else { // Default functionality    else { // Default functionality
489      $obj_def = call_user_func($class .'_class_define');      $obj_def = call_user_func($class .'_class_define');
490      $table = isset($obj_def['table']) ? $obj_def['table'] : $class;      $table = isset($obj_def['table']) ? $obj_def['table'] : $class;
491    
492        if ($obj->is_new) {
493          // Insert a new object
494          $obj->$id_name = db_next_id("{". $table ."}_id");
495        }
496    
497      $column_names = array();      $column_names = array();
498      $column_placeholders = array();      $column_placeholders = array();
499      $column_assignments = array();      $column_assignments = array();
# Line 513  function object_driver_save(&$obj) { Line 513  function object_driver_save(&$obj) {
513      }      }
514      $data[] = $obj->$id_name;      $data[] = $obj->$id_name;
515      if ($obj->is_new) {      if ($obj->is_new) {
516        db_query("INSERT INTO {$table} (". implode(', ', $column_names) .", $id_name) VALUES (". implode(', ', $column_placeholders) .', %d)', $data);        db_query("INSERT INTO {". $table ."} (". implode(', ', $column_names) .", $id_name) VALUES (". implode(', ', $column_placeholders) .', %d)', $data);
517      }      }
518      else {      else {
519        db_query("UPDATE {$table} SET ". implode(', ', $column_assignments) ." WHERE $id_name = %d", $data);        db_query("UPDATE {". $table ."} SET ". implode(', ', $column_assignments) ." WHERE $id_name = %d", $data);
520      }      }
521    }    }
522    if ($obj->is_new) {    if ($obj->is_new) {
# Line 565  function object_driver_delete($obj) { Line 565  function object_driver_delete($obj) {
565      $table = isset($obj_def['table']) ? $obj_def['table'] : $class;      $table = isset($obj_def['table']) ? $obj_def['table'] : $class;
566      $id_name = $class .'_id';      $id_name = $class .'_id';
567    
568      db_query("DELETE FROM {%s} WHERE %s = %d", $table, $id_name, $obj->$id_name);      db_query("DELETE FROM {". $table ."} WHERE %s = %d", $id_name, $obj->$id_name);
569      if (isset($obj->type)) {      if (isset($obj->type)) {
570        if (function_exists($obj->type .'_'. $class .'_delete')) {        if (function_exists($obj->type .'_'. $class .'_delete')) {
571          call_user_func($obj->type .'_'. $class .'_delete', $obj);          call_user_func($obj->type .'_'. $class .'_delete', $obj);

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.2