DooPHP - fastest MVC based PHP framework

Learn More

DooPHP Model

The definitive guide to DooPHP

Discussions

Have a question? Post it at the forum.

DooPHP Model

Models are PHP classes that are designed to work with information in your database. In DooPHP model files are located in SITE_PATH/protected/model.

Say you have a table in DB named user:



And a basic model class might look like:

class User{
    public $id;
    public $username;
    public $pwd;
    public $group;
    public $vip;

    public $_table = 'user';
    public $_primarykey = 'id';
    public $_fields = array('id', 'username', 'pwd', 'group', 'vip');
}

Your Models do not have to extend any parent class in order to use the ORM in DooPHP. All you have to do is to define the field names as class properties in the Model class and define $_table(table name), $_primarykey and $_fields(field names in array)

To use the database ORM you have to configure your database settings too. Open up db.conf.php (in SITE_PATH/protected/config), and write:

$dbconfig['dev'] = array('localhost', 'db', 'root', '1234', 'mysql',true);

And remember to setup the DB in index.php

Doo::db()->setDb($dbconfig, 'dev');

Now, having the database setup, you can even generate Model class from your database.

This save you tedious work to write Model classes manually. Model validation rules will be generated along as well!


Database CRUD

Selecting data

You can try getting data from the database by using the ORM in the controller:

class MyController extends DooController {
    
    function testDb(){
        //This returns an array of user objects
        Doo::db()->find('User');
        
        //Or this syntax
        $this->db()->find('User');

        //You can create a User object
        Doo::loadModel('User');
        $user = new User;
        $user->username = 'david';

        //The result is A user object
        Doo::db()->find( $user, array('limit'=>1) );
    }

}

Inserting data

To insert a new record to a table, you need to create the Model object first:

class MyController extends DooController {
    
    function testDb(){
        Doo::loadModel('User');

        $user = new User;
        $user->username = 'david';
        $user->pwd = 'absd676328';
        $user->group = 'member';

        //The result is the last inserted Id
        $result = Doo::db()->insert( $user );
    }

}

Updating data

Here's an example that search for a user and edit the password of the user:

class MyController extends DooController {
    
    function testDb(){
        Doo::loadModel('User');
        
        $user = new User;
        $user->id = 3;
        $options['limit'] = 1;
        
        //The result is the User object with ID = 3
        $user = Doo::db()->find( $user, $options );
        
        //Change the password
        $user->pwd = 'mypwd';
        Doo::db()->update( $user );
    }

}

Deleting data

Here's an example that search for a user by ID and delete the record:

class MyController extends DooController {
    
    function testDb(){
        Doo::loadModel('User');
        
        $user = new User;
        $user->id = 3;
        Doo::db()->delete( $user );
    }

}

More advanced database operations such as table relationship will be covered in a later section.