2012-03-02 4 views
2

데이터베이스에서 사용자 정보를 얻기 :탱크 인증 - 나는 데이터베이스에서 사용자 데이터를 철회하는 기능 만든

function user() 
{ 
    $user_id = $this->ci->session->userdata('user_id'); 

    if (!is_null($user = $this->ci->users->get_user_by_id($user_id, TRUE))) { 
     return $this->ci->users->get_user($user_id); //model 
    } 
    return FALSE; 
} 

내가 어떤 컨트롤러에서 이런 식으로 사용하고 있습니다

$this->tank_auth->user()->field.

나를 위해 편리하지 않으므로 이렇게해야 사용자 데이터를 얻을 수 있습니까?

$this->user->field 또는 $this->tank_auth->user->field. 위의 기능에 대한

내 두 번째 질문은,이 기능입니다 :

function is_admin() 
{ 
    if ($this->user()->who) 
    { 
     return $this->user()->who === "ADMIN"; 
    } 
    return FALSE; 
} 

그것은 던져 오류 수행합니다

A PHP Error was encountered 

Severity: Notice 

Message: Trying to get property of non-object 

Filename: libraries/Tank_auth.php 

Line Number: 335 

가 (라인 335 if ($this->user()->who)입니다).

왜 이런 일이 발생합니까? 두 함수 모두 tank_auth의 라이브러리에 위치하므로 잘 작동한다고 가정합니다.

도움을 주시면 감사하겠습니다.

function get_user_profile_by_id($user_id){ 
    $this->db->where('user_id', $user_id); 

    $query = $this->db->get($this->profile_table_name); 
    if ($query->num_rows() == 1) return $query->row(); 
    return NULL; 
} 

당신이 다음 예를 들어, 사용자 프로필 테이블에 VARCHAR 필드에서는 UserClass를 추가하고 사용자가 함께 특정 클래스에 속하는지 확인할 수 있습니다 : 당신이 users.php 모델에이 방법을 추가하면

+0

$ this-> tank_auth-> user()를 이와 같은 변수로 설정할 수 있습니다. $ user = $ this-> tank_auth-> user(); 이렇게하면 $ user-> field와 같은 값에 액세스 할 수 있습니다. is_admin 메소드를 사용하여 수행하려는 작업은 무엇입니까? –

+0

@DougWallace'$ this-> tank_auth-> user() -> who'가 ADMIN인지 아닌지 확인하고 싶습니다. – Cyclone

답변

0

$profile = $this->users->get_user_profile_by_id($this->tank_auth->get_user_id()); 
if ($profile->userclass = "ADMIN") { 
... 
} 

희망이 있습니다.

관련 문제