Posts Tagged ‘database’

Profiling and DB Profiling with DooPHP

If you have read the previous tutorial on logging you will find that profiling with DooPHP framework is relatively similar to the way you log messages.

Performance profiling can be used to measure the time & memory needed for the specified code blocks and find out what the performance bottleneck is. Instead of calling log() you change it to beginProfile() and endProfile(). We need to mark the beginning and the end of each code block by inserting the following methods:

Doo::logger()->beginProfile('block_id_here');
//...everything here will be profiled
Doo::logger()->endProfile('block_id_here');

Code blocks need to be nested properly. A code block cannot intersect with another. It must be either at a parallel level or be completely enclosed by the other code block.

All of the profiled results can be organized in category, simply pass in another parameter at the end of the method beginProfile():

//default category is 'application'
Doo::logger()->beginProfile('id', 'editpost');
//...everything here will be profiled
Doo::logger()->endProfile('id');

To retrieve the profiled results, you called getProfileResult(). You have to pass in the block ID as parameter and you will get an associative array which shows you the time and memory used when processing the code block.

Doo::logger()->beginProfile('block_id');
//...everything here will be profiled
Doo::logger()->endProfile('block_id');
$result = Doo::logger()->getProfileResult('block_id');

To view the profiled results, you just have to call showLogs(). By default it will return a neatly

More »

Managing your database(s) with DooPHP

This tutorial will demonstrate how to use the DooManageDb and DooUpdateDb classes to manage your projects database.

These classes have been developed to allow your application(s) to be independent of the database your users choose to use by allowing standard database definition to be used to define your database which is then translated into the SQL needed for the database engine the user is using. It also supports easy upgrading (and downgrading) of the database to allow for better version control support and rolling out of updates and unit testing.

In order to follow this tutorial you will need to use the latest version of DooPHP avaliable form the SVN repository (or version 1.3 or above – not out at the time of writing). The current implementation only supports MySQL and PgSQL at the moment so you will also need one of these 2 databases in order to follow the guide.

The first thing we need to do is to create a simple controller to run our tests from. Therefore create a new controller in your protected/controller/ folder. In this example I will use the controller “DatabaseController”. Now enter the following into the controller:

<?php
class DatabaseController extends DooController {

	public function updateDatabase() {

		echo '<h1>Update Database to Head</h1>' . PHP_EOL;

		/* DB Updater
More »

Using DooModel for Database Operations

If you have ever read DooPHP guide on model, you will see that a basic Model class does not have to extend any superclass.

A basic Model class looks like this:

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

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

With the basic Model class, you can search for a database record by:

//$this->db()->find('User');  is the same
Doo::db()->find('User');

//search for one record
Doo::db()->find('User', array('limit'=>1) );

//search for a user named 'david'
Doo::db()->find('User', array('where'=>"uname='david'", 'limit'=>1) );

//using prepared statement to avoid sql injection
Doo::db()->find('User', array(
                    'where' => 'user=?',
                    'param' => array($_GET['name']),
                    'limit' => 1
                )
           );

//Or simply use this for shorter code.
Doo::loadModel('User');
$u = new User;
$u->uname = $_GET['name'];
$result = Doo::db()->find($u, array('limit'=>1));

Although the above code is pretty straightforward, we can make it even shorter and cleaner. First of all, we would need to have our Models to extend the DooModel class. We will have our Model class as the code below, notice the constructor:

Doo::loadCore('db/DooModel');
class User extends DooModel {
    public $id;
    public $uname;
    public $pwd;
    public $group;
    public $vip;

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

    function __construct(){
         parent::$className = __CLASS__;
    }
}

By

More »

Create a simple To Do List in DooPHP – Part 1

Introduction

This tutorial will guide you through all the steps required to get started using DooPHP through the creation of a simple To Do List Application. The tutorial is split into a number of sections and you will need to read the guide from the begining in order to understand whats going on.

  • Part 1 – Getting Started
  • Part 2 – The Signup Form
  • Part 3 – User Authentication – Coming Soon
  • Part 4 – The To Do List – Coming Soon
  • Part 5 – Adding AJAX Functionality – Coming Soon

Our Objective for this Tutorial

In this tutorial we will be creating a simple To Doo List Manager Web Application and through the course of the tutorial we will be covering the following topics:

  • Obtaining and Deploying the Base DooPHP Application
  • Setting up the MySQL Database
  • Creating the Models
  • Creating the To Do Manager Application
    • Signup Form
    • User Authentication
    • User Home Page
    • Task Actions
    • Enhancing you application with AJAX

You can view a demo of the finished project here. Some screen shots of the final application are also included below:

TODO: IMAGES TO GO IN HERE WHEN I FINISH!

TODO: UPDATE THE DEMO LINK ABOVE

More »