Welcome Guest, Not a member yet? Register   Sign In
stored the user information so other controllers/views can use
#1

[eluser]b3yang[/eluser]
hey guys.. am sure this question has been asked before but I haven't able to find anything that's clear to me.
so essentially I have a site which requires user login, and after the validation process is completed, I want to fill in the User object with attrbiutes. (say user preference etc.)
that being said, in order to avoid hitting database again every time a page loads, I want to store these user attributes somewhere...
believe there are two alternatives:
1. stuff everything in session
2. create global variables..
My question is, is it possible for me to store the whole object in session or in global variables?

I found a very good tutorial here which does exactly what I wanted
http://www.phpandstuff.com/articles/code...user-login
however doctrine is not an option for me..

essentially refering to the tutorial I am wondering if I can do..

$u = Doctrine::getTable('User')->findOneByUsername($username)
self::$user = $u;

then in view call
Current_User::user()->username withtout using doctrine..
any help is much appreciated folks.
#2

[eluser]b3yang[/eluser]
so am thinking of doing this..
1. create a model called user which uses activerecord to do validation and populate user attribute
2. create a singleton model called current user_similar to the tutorial and which it, instantiate a user model and set it as the static variable..
3. so call from view currentuser::user()->getAttribute();

am wondering is this even possible and do all the attributes in user have to be static as well?
sorry guys.. pretty noob in codeigniter here...and my 1st post.. fingers crossed for a good answer..
#3

[eluser]Colin Williams[/eluser]
How about in your constructor you do

Code:
$this->load->vars('cur_user' => $this->user_model->get_current_user());

Then in any of your views you have $cur_user->email, $cur_user->name, etc
#4

[eluser]b3yang[/eluser]
thanks for your reply colin, and that sounds like a good idea..
just to confirm though, are you suggesting I do

Code:
<?php
class user extends Model{
    public function user()
    {
        //populate user attribute from database;    
    }
}

then

Code:
<?php
class user_model extends Model {
    private static $cur_user;
    private function __construct() {}
    public static function get_current_user() {
        if(!isset(self::$cur_user)) {
            self::$cur_user =$this->load->model('user');
        }
        return self::$cur_user;
    }
    public function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }

}

also, am just wondering, is this a standard practice?
again, thanks so much for your help..
#5

[eluser]Colin Williams[/eluser]
Looks to be about all there. I build my models a bit differently, in that the model class does not represent one entity of a user.

Here's a copy of one of my User_models

Code:
function get_user($uid = FALSE)
   {
      $user = FALSE;
      
      if (!$uid)
      {
         $uid = $this->ci->session->userdata('uid');
      }
      
      if (!isset($this->users[$uid]))
      {
         // Query the database for the user and respective attributes
         $this->ci->db->select($this->config['user_table'] . '.uid AS uid, created, email, password, status, ' . $this->config['attribute_table'] . '.uid AS a_uid, `key`, `value`');
         $this->ci->db->where($this->config['user_table'] . '.uid', $uid);
         $this->ci->db->join($this->config['attribute_table'], $this->config['attribute_table'] . '.uid = ' . $this->config['user_table'] . '.uid', 'left');
         $query = $this->ci->db->get($this->config['user_table']);
         if ($query->num_rows() > 0)
         {
            // Build the user object and cache user
            $user = $this->_build_users($query->result());
            $this->users[$uid] = $user;
         }
      } else {
         // Grab the user from the cache
         $user = $this->users[$uid];
      }
      
      return $user;
   }

So essentially when I call User_model::get_user() with no user id, it grabs one from the session.
#6

[eluser]b3yang[/eluser]
aah i see.. that make sense to me...make my life easier as well since only one model is needed..
think am almost there.. so then your users varaible is a array containing all the logged in users and you remove them as they logout?
#7

[eluser]Colin Williams[/eluser]
Quote:so then your users varaible is a array containing all the logged in users and you remove them as they logout?

No, $this->users is a local cache of users that prevents me from hitting the database each time the function is called, or if that user object had been requested previously in some fashion.
#8

[eluser]b3yang[/eluser]
sorrie for the noobness...how do i setup a local cache?
i.e. how do you create and manage the local cache
thanks again.
#9

[eluser]b3yang[/eluser]
alright.. did some research and looks likes this is a whole diff topic...
am gonna stick to a static array for now.... and probably post the cache question under a diff thread when I get to it..thanks colin
cheers
#10

[eluser]Colin Williams[/eluser]
It's pretty simple, actually. I just have an array of fully built user objects, where the array index is the unique identifier of the object (primary key in the database).

Code:
class User extends Model {

  var $users = array();

}

Then you just make sure your get_* functions first check in this array before going to the database, and after going to the database, you add to the array. It's just a good way to make sure you aren't running queries for data you already have.




Theme © iAndrew 2016 - Forum software by © MyBB