RFC/SGL_UserRefactoring: Session_and_DA_UserWithout_Dataobject.diff

File Session_and_DA_UserWithout_Dataobject.diff, 7.3 kB (added by mstahv, 2 years ago)

Result of short prototyping session. Removed Dataobject usage from Session and DA_User and got a significant performance increase.

  • lib/SGL/Session.php

    old new  
    143143 
    144144        //  if user id is passed in constructor, ie, during login, init user 
    145145        if ($uid > 0) { 
    146             require_once 'DB/DataObject.php'; 
    147             $sessUser = DB_DataObject::factory($conf['table']['user']); 
    148             $sessUser->get($uid); 
     146            require_once SGL_MOD_DIR . '/user/classes/DA_User.php'; 
     147            $sessUser = new User($uid); 
    149148            $this->_init($sessUser); 
    150149 
    151150        //  if session doesn't exist, initialise 
  • modules/user/classes/DA_User.php

    old new  
    3838// +---------------------------------------------------------------------------+ 
    3939// $Id: DA_User.php,v 1.14 2005/06/21 23:26:24 demian Exp $ 
    4040 
    41 require_once 'DB/DataObject.php'; 
    42  
    4341//  role sync constants 
    4442define('SGL_ROLESYNC_ADD',              1); 
    4543define('SGL_ROLESYNC_REMOVE',           2); 
     
    102100        $ok = $oUser->insert(); 
    103101 
    104102        if (!$ok) { 
    105             return PEAR::raiseError('Problem inserting user DataObject'); 
     103            return PEAR::raiseError('Problem inserting user'); 
    106104        } 
    107105        //  assign permissions associated with role user belongs to 
    108106        //  first get all perms associated with user's role 
     
    214212     * @param integer   $id optional user id 
    215213     * @return object   A DataObjects user object 
    216214     */ 
    217     function getUserById($id = null) 
    218     { 
    219         $oUser = DB_DataObject::factory($this->conf['table']['user']); 
     215    function getUserById($id = null) { 
    220216        if (!is_null($id)) { 
    221             $oUser->get($id); 
     217            $oUser = new User($id); 
     218        } else { 
     219            $oUser = new User(); 
    222220        } 
    223221        return $oUser; 
    224222    } 
     
    11071105    function isUniqueUsername($username) 
    11081106    { 
    11091107        if (isset($username)) { 
    1110             $oUser = DB_DataObject::factory($this->conf['table']['user']); 
    1111             $oUser->whereAdd("username = '$username'"); 
    1112             $numRows = $oUser->find(); 
    1113  
    1114             //  return false if any rows found 
     1108            $numRows = $this->dbh->getOne("SELECT COUNT(usr_id)  
     1109                FROM {$this->conf['table']['usr']}  
     1110                WHERE username = '$username'"); 
    11151111            return (boolean)$numRows == 0; 
    11161112        } 
    11171113    } 
     
    11261122    function isUniqueEmail($email) 
    11271123    { 
    11281124        if (isset($email)) { 
    1129             $oUser = DB_DataObject::factory($this->conf['table']['user']); 
    1130             $oUser->whereAdd("email = '$email'"); 
    1131             $numRows = $oUser->find(); 
    1132  
    1133             //  return false if any rows found 
     1125            $numRows = $this->dbh->getOne("SELECT COUNT(usr_id)  
     1126                FROM {$this->conf['table']['usr']}  
     1127                WHERE email = '$email'"); 
    11341128            return (boolean)$numRows == 0; 
    1135         } 
     1129        } else {return false;} 
    11361130    } 
    11371131 
    11381132    /** 
     
    11581152 
    11591153    //OrgPreferenceMgr::_updateAll 
    11601154} 
     1155 
     1156class User { 
     1157    var $usr_id           = 0; 
     1158    var $organisation_id  = 0; 
     1159    var $role_id          = 0;   
     1160    var $username         = ''; 
     1161    var $passwd           = ''; 
     1162    var $first_name       = ''; 
     1163    var $last_name        = ''; 
     1164    var $teleph           = ''; 
     1165    var $mobile           = ''; 
     1166    var $email            = ''; 
     1167    var $addr_1           = ''; 
     1168    var $addr_2           = ''; 
     1169    var $addr_3           = ''; 
     1170    var $city             = ''; 
     1171    var $region           = ''; 
     1172    var $country          = ''; 
     1173    var $post_code        = ''; 
     1174    var $is_email_public  = 0; 
     1175    var $is_acct_active   = 0; 
     1176    var $security_questio = ''; 
     1177    var $security_answer  = ''; 
     1178    var $date_created     = '0000-00-00'; 
     1179    var $created_by       = 0; 
     1180    var $last_updated     = '0000-00-00'; 
     1181    var $updated_by      = ''; 
     1182     
     1183    function User($id = false) { 
     1184        $this->dbh = & SGL_DB::singleton(); 
     1185        $c = &SGL_Config::singleton(); 
     1186        $this->conf = $c->getAll(); 
     1187         
     1188        if($id) { 
     1189            $aDbobject = $this->dbh->getAssoc("SELECT * FROM {$this->conf['table']['user']} WHERE usr_id = $id"); 
     1190            foreach (get_object_vars($aDbobject[$id]) as $prop => $val) { 
     1191                $this->$prop = $val; 
     1192            } 
     1193            unset($dbobject); 
     1194        } 
     1195    } 
     1196     
     1197    function setFrom($obj) { 
     1198        foreach (get_object_vars($obj) as $prop => $val) { 
     1199            $this->$prop = $val; 
     1200        } 
     1201    } 
     1202     
     1203    function update() { 
     1204        $ok = $this->dbh->query(" 
     1205            UPDATE {$this->conf['table']['user']} 
     1206            SET 
     1207            organisation_id  = $this->organisation_id, 
     1208            role_id          = $this->role_id, 
     1209            username         = '$this->username', 
     1210            passwd           = '$this->passwd', 
     1211            first_name       = '$this->first_name', 
     1212            last_name        = '$this->last_name', 
     1213            telephone        = '$this->teleph', 
     1214            mobile           = '$this->mobile', 
     1215            email            = '$this->email', 
     1216            addr_1           = '$this->addr_1', 
     1217            addr_2           = '$this->addr_2', 
     1218            addr_3           = '$this->addr_3', 
     1219            city             = '$this->city', 
     1220            region           = '$this->region', 
     1221            country          = '$this->country', 
     1222            post_code        = '$this->post_code', 
     1223            is_email_public  = '$this->is_email_public', 
     1224            is_acct_active   = '$this->is_acct_active', 
     1225            security_question= '$this->security_question', 
     1226            security_answer  = '$this->security_answer', 
     1227            date_created     = '$this->date_created', 
     1228            created_by       = '$this->created_by', 
     1229            last_updated     = '$this->last_updated', 
     1230            updated_by       = '$this->updated_by' 
     1231            WHERE  
     1232                usr_id   =   $this->usr_id 
     1233                 
     1234        "); 
     1235    } 
     1236    function insert() { 
     1237        $ok = $this->dbh->query(" 
     1238            INSERT INTO {$this->conf['table']['user']} 
     1239            VALUES ( 
     1240                $this->usr_id           , 
     1241                $this->organisation_id  , 
     1242                $this->role_id          , 
     1243                '$this->username     , 
     1244                '$this->passwd', 
     1245                '$this->first_name', 
     1246                '$this->last_name', 
     1247                '$this->telephone', 
     1248                '$this->mobile', 
     1249                '$this->email', 
     1250                '$this->addr_1', 
     1251                '$this->addr_2', 
     1252                '$this->addr_3', 
     1253                '$this->city', 
     1254                '$this->region', 
     1255                '$this->country', 
     1256                '$this->post_code', 
     1257                '$this->is_email_public', 
     1258                '$this->is_acct_active', 
     1259                '$this->security_question', 
     1260                '$this->security_answer', 
     1261                '$this->date_created', 
     1262                '$this->created_by', 
     1263                '$this->last_updated', 
     1264                '$this->updated_by' 
     1265                 
     1266            ); 
     1267        "); 
     1268        if(PEAR::isError($ok)) { 
     1269            return false; 
     1270        } else { 
     1271            return true; 
     1272        } 
     1273    } 
     1274} 
    11611275?>