Welcome Guest, Not a member yet? Register   Sign In
[In the Works] Datamapper ORM Class
#1

[eluser]Michael Wales[/eluser]
Many of you have noticed that I haven't been posting much - needless to say I have been insanely busy. Only one more month in the desert then it is back home with the family.

In the small amount of free time I still have I've been working on quite a few projects. My favorite of which is my Datamapper ORM Class although I'm pretty sure it doesn't meet the Datamapper pattern completely.

This is, as of now, not yet ready for release - but here's a taste of what is to come.

Here's a model, let's say - for a user:

model/user.php
Code:
<?php
class User extends Datamapper {

    var $username;
    var $email;
    var $password;
    var $salt;
    
    var $validation = array(
        'username' => array('required', 'unique', 'min' => 3, 'max' => 20, 'trim'),
        'email' => array('required', 'unique', 'valid_email', 'trim'),
        'password' => array('required', 'min' => 3, 'max' => 20, 'constraint' => 120));
        
    function save_salt() {
        $this->load->helper('string');
        if (!isset($this->salt)) {
            $this->salt = random_string('alnum', 10);
        }
    }
    
    function save_password() {
        $this->load->helper('security');
        if (!isset($this->salt)) {
            $this->save_salt();
        }
        $this->password = dohash($this->salt . $this->password);
    }
}

One of the first things you'll notice is the validation array. Yes, Validation is being moved 100% completely into the model. All of the CodeIgniter standard validation rules are in-place, in addition to a few new ones (unique can be seen above, which is self explanatory).

You also should note the constructor as it's passing an array to it's parent class. Datamapper models are designed to really be used as objects, not just a class to make your organization a bit better. More on this in the Controller area below.

Finally, you'll see the method save_salt(). As of now only data modification upon save is implemented not sure if there is really a need for it anywhere else. Some might suggest upon "getting" the data but I dgress - isn't the View in charge of making data pretty?

A few more bits you might not pick up from reviewing the code within the model:

- There is a class variable $salt but there are no validation rules to match! Datamapper understands this as meaning "this is a user property but it can't be changed via a form - only in the code." This allows us to use a handy little function later to just pull in all POST data to the model and believe it will be okay (no worrying about whether a malicious user has added an admin ENUM to your form hoping you just accept everything).

- All models have an id, created_on, updated_on field that is automatically updated and managed by the Datamapper model.

- Currently enabled in the model (because it is not explicitly disabled) is database analysis. The Datamapper automatically reviews your model, determines what the table structure should be, then checks to see if the table is created and matches the correct structure. In plain English: you write your model, Datamapper automatically creates the table for you. You need to add a new parameter to your model? Just write it in. No more hopping back and forth between code and phpMyAdmin. There's still a lot of work to do on this front, primarily more intelligent column type identification and better parameters within the model. I'm trying to think of a graceful way to accomplish this - right now it's just part of the validation array (see the constraint key within password, that tells Datamapper to make the field limit 120 characters).

Controllers
Code:
<?php
class Users extends Controller {

    function Users() {
        parent::Controller();
        $this->output->enable_profiler(TRUE);
    }
    
    function index() {
        $this->datamapper->model('user');
        // Get the user whose username == walesmd and echo his email
        $u = new User(array('username'=>'walesmd'));
        echo $u->email;
        
        // Get all users who are admins
        $u = new User(array('type'=>'admin'));
        // We fully expect more than one user, so we foreach the all class variable
        foreach ($u->all as $users) {
            echo $u->username;
        }

        // Let's create a new user
        $u = new User();
        $u->username = 'dallard';
        $u->password = 'robots';
        $u->save();

        // Let's create another new user - this time after a form has been submitted
        // Form Input names are: signup[username], signup[password], and signup[email]
        $u = new User();
        $u->fromForm('signup');
        $u->save();

        // Let's update user #1's email address
        $u = new User(array('id'=>1));
        $u->email = '[email protected]';
        $u->save();
    }
}

Lots of stuff here but it should be pretty easy to understand. When creating a new Datamapper object you can pass an array - this will be used within a CodeIgniter ActiveRecord get_where() query that will return all objects that match that query.

If you are expecting more than one result to come back, go ahead and foreach through the Models all variable. Note: This will still work if you only return one object, Datamapper is designed around your expectations. If you expect more than one, loop it. If you expect only one - don't.

We created a new user, which was pretty simple.


Messages In This Thread
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 06:50 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 06:56 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 07:26 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 07:31 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 07:36 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 09:00 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 09:31 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-27-2008, 09:34 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-28-2008, 02:10 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-28-2008, 09:52 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-28-2008, 12:32 PM
[In the Works] Datamapper ORM Class - by El Forum - 08-28-2008, 01:03 PM
[In the Works] Datamapper ORM Class - by El Forum - 08-28-2008, 09:13 PM
[In the Works] Datamapper ORM Class - by El Forum - 08-29-2008, 04:27 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-29-2008, 04:37 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-29-2008, 06:25 AM
[In the Works] Datamapper ORM Class - by El Forum - 08-29-2008, 06:59 PM
[In the Works] Datamapper ORM Class - by El Forum - 08-30-2008, 08:09 AM
[In the Works] Datamapper ORM Class - by El Forum - 09-01-2008, 06:24 AM
[In the Works] Datamapper ORM Class - by El Forum - 09-01-2008, 08:11 AM
[In the Works] Datamapper ORM Class - by El Forum - 10-21-2008, 06:25 PM
[In the Works] Datamapper ORM Class - by El Forum - 10-30-2008, 01:44 PM
[In the Works] Datamapper ORM Class - by El Forum - 10-30-2008, 01:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB