Welcome Guest, Not a member yet? Register   Sign In
variables doesn't get saved
#1

[eluser]TomMRiddle[/eluser]
I use datamapper and at some point we had this working but it have stopped working. only fields that gets saved is the ones whose values have been set by the controller.

the salt, encrypted_email and encrypted_password doesn't get saved.
User Model:
Code:
<?php
class User extends DataMapper {
    
    //var $has_many = array('mymovie');
    //var $has_one = array('subscribed');
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    //private $encrypted_password;
    var $validation = array(
        'name' => array(
            'label' => 'name',
            'rules' => array('unique', 'required', 'trim', 'alpha_dash', 'min_length' => 5, 'max_length' => 20),
        ),
        'password' => array(
            'label' => 'Password',
            'rules' => array('required', 'min_length' => 6, '_encrypt_password'),
        ),
        'confirm_password' => array(
            'label' => 'Confirm Password',
            'rules' => array('required', 'matches' => 'password'),
        ),
        'email' => array(
            'label' => 'Email Address',
            'rules' => array('required', 'trim', 'unique', 'valid_email', '_encrypt_email')
        )
    );
    
    function login()
    {
        // Create a temporary user object
        $u = new User();

        // Get this users stored record via their username
        $u->where('email', $this->email)->get();

        // Give this user their stored salt
        $this->salt = $u->salt;

        // Validate and get this user by their property values,
        // this will see the 'encrypt' validation run, encrypting the password with the salt
        $this->validate()->get();

        // If the username and encrypted password matched a record in the database,
        // this user object would be fully populated, complete with their ID.

        // If there was no matching record, this user would be completely cleared so their id would be empty.
        if (empty($this->id))
        {
            // Login failed, so set a custom error message
            $this->error_message('login', 'name or password invalid');

            return FALSE;
        }else if(!$this->confirmed)
        {
            $this->error_message('login', 'email adresse not confirmed');

            return FALSE;
        }
        else
        {
            // Login succeeded
            return TRUE;
        }
    }
    
    // Validation prepping function to encrypt passwords
    // If you look at the $validation array, you will see the password field will use this function
    private function _encrypt($field)
    {
        // Don't encrypt an empty string
        if (!empty($this->{$field}))
        {
            // Generate a random salt if empty
            if (empty($this->salt))
            {
                $this->salt = md5(uniqid(rand(), true));
            }

            return sha1($this->salt . $this->{$field});
        }
    }

    private function _encrypt_email($field)
    {
    $this->encrypted_email = _encrypt($field);
    }

    private function _encrypt_password($field)
    {
    $this->encrypted_password = _encrypt($field);
    }
}

/* End of file user.php */
/* Location: ./application/models/user.php */
?>

Controller:
Code:
<?php

class Users extends Controller {

    function __construct()
    {
    parent::__construct();
    }

    function index()
    {
    // Let's create a user
    $u = new User();
    $u->name = 'Fred_Smith';
    $u->password = 'apples';
    $u->confirm_password = 'apples';
    $u->email = '[email protected]';
    
    // And save them to the database (validation rules will run)
    if ($u->save())
    {
        // User object now has an ID
        echo 'ID: ' . $u->id . '<br />';
        echo 'name: ' . $u->name . '<br />';
        echo 'Email: ' . $u->email . '<br />';

        // Not that we'd normally show the password, but when we do, you'll see it has been automatically encrypted
        // since the User model is setup with an encrypt rule in the $validation array for the password field
        echo 'Password: ' . $u->password . '<br />';
        echo 'ePassword: ' . $u->encrypted_password . '<br />';
        echo 'Salt: ' . $u->salt . '<br />';
    }
    else
    {
        // loop through the error's all list
        foreach ($u->error->all as $error)
        {
        echo $error;
        }
    }


    // You can delete multiple relations of different types in the same way you can save them
    // Now that we're done with the user, let's delete him
    //$u->delete();

    // When you delete the user, you delete all his relations with other objects.  DataMapper does all the tidying up for you :)
    }
}

/* End of file users.php */
/* Location: ./application/controllers/users.php */
?&gt;


Messages In This Thread
variables doesn't get saved - by El Forum - 01-27-2011, 03:27 AM
variables doesn't get saved - by El Forum - 02-06-2011, 01:43 PM
variables doesn't get saved - by El Forum - 05-05-2011, 09:47 PM



Theme © iAndrew 2016 - Forum software by © MyBB