Welcome Guest, Not a member yet? Register   Sign In
CI and Doctrine - How to reference CI Classes from within Doctrine Class
#1

[eluser]iDVB[/eluser]
Ok, so maybe what I'm trying to do...I shouldn't even do...but I'm really looking for the right way to do this.

I've integrated Doctrine and CI and I have a basic User class like this:
Code:
<?php

class User extends BaseUser
{    
    
    public function __construct()
    {
        parent::__construct();
        //$this->ci =& get_instance();
        //$this->load->config('redux_auth');
    }
    
    public function setPassword($password)
    {
        return $this->_set('password', $this->hash_password($password));
    }

    public function checkPassword($password = false)
    {
        if ($password === false)
        {
            return false;
        }
    
        $password_hashed = $this->hash_password_db($password);
        
        echo "password_hashed ". $password_hashed ."\n";
        echo "this-password ". $this->password ."\n";
        
        if ($password_hashed == $this->password) {
            return true;
        }
        else
        {
            return false;
        }
    }

    private function hash_password($password = false)
    {
        $salt_length = 10;
        
        if ($password === false)
        {
            return false;
        }
        
        $salt = $this->salt($salt_length);
        
        $password = $salt . substr(sha1($salt . $password), 0, -$salt_length);
        
        return $password;        
    }
    
    private function hash_password_db($password = false)
    {  
        $salt_length = 10;
        
        $salt = substr($this->password, 0, $salt_length);

        $password = $salt . substr(sha1($salt . $password), 0, -$salt_length);
        
        return $password;
    }
    
    private function salt($length = 10)
    {
        return substr(md5(uniqid(rand(), true)), 0, $length);
    }

    public function login($password = false)
    {
        if ($password === false)
        {
            return false;
        }
    
        if ($this->checkPassword($password))
           {
            if (!empty($this->activation_code)) { return false; }
    
               //$this->ci->session->set_userdata('email',  $this->email);
               return true;
           }

        return false;
    }

}

the commented line within "checkPassword()" is my issue. I want to use the CI Session class to create a session for that user...however CI does not exist in this class yet. So I tried to include it in the "__construct" like this:

Code:
public function __construct()
{
    parent::__construct();
    $this->ci =& get_instance();
}

but it seems this does not work either? Can anyone offer advice how to get this type of situation to work properly?

Cheers
#2

[eluser]iDVB[/eluser]
bump
#3

[eluser]wiredesignz[/eluser]
Try, $session = load_class('Session');
#4

[eluser]iDVB[/eluser]
Thanks for the reply wiredesignz ... I'll try that.

Sorry, just a quick question regarding your suggestion.
Is that a quickfix kind of a suggestion or is this what all doctrine/CI users do to reference CI classes from within a doctrine class?

Does this mean that I will have to manually include each class and that things such as CI helpers etc are not avail?

Cheers
#5

[eluser]wiredesignz[/eluser]
It has nothing to do with Doctrine. Your issue seems to be that the $CI object does not exist yet and cannot be referenced using get_instance(). Using load_class() is the core function used to load libraries for CI itself, so this should work for you also.

Other features will be available after the controller ($CI) is instantiated and can be accessed by using get_instance().
#6

[eluser]iDVB[/eluser]
So this is probably to do with my implementation of Doctrine? Maybe I need to install it into CI as a plugin?
#7

[eluser]iDVB[/eluser]
I guess one of the questions I'm asking is...am I "supposed" to be able to reference CI classes as per normal from within Doctrine classes? So maybe this is just a poor Doctrine implementation on my part?
#8

[eluser]iDVB[/eluser]
bump

Is there really no one out there that can offer some tried and true incite into how to integrate Doctrine and CI at a code level as mentioned above?

How should/would/could someone have a Doctrine User class/model that can log that user in by using the CI Session Class?
#9

[eluser]brianw1975[/eluser]
I don't understand why you would want to mix and match handling basic user log in. If you want to use CI sessions, then have CI do all of the login functions and pass necessary information to Doctrine to check/validate usernames, passwords, etc and return true/false, etc.

what you are doing is like trying to integrate login functions with [phpBB3] and trying to get [phpBB3] to use the CI sessions...
#10

[eluser]iDVB[/eluser]
You bring up a very good point. What I was trying to do was remodel redux_auth so that everything was using doctrine. Since redux_auth had a model that had a login() function I was trying to move that functionality into a doctrine model instead. Hence my proposed solution.

Maybe I need to rethink this a bit.

Cheers




Theme © iAndrew 2016 - Forum software by © MyBB