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:
01.
class User{
02.
public $id;
03.
public $uname;
04.
public $pwd;
05.
public $group;
06.
public $vip;
07.
08.
public $_table = ‘user’;
09.
public $_primarykey = ‘id’;
10.
public $_fields = array(‘id’, ‘uname’, ‘pwd’, ‘group’, ‘vip’);
11.
}
With the basic Model class, you can search for a database record by:
01.
//$this->db()->find(‘User’); is the same
02.
Doo::db()->find(‘User’);
03.
04.
//search for one record
05.
Doo::db()->find(‘User’, array(‘limit’=>1) );
06.
07.
//search for a user named ‘david’
08.
Doo::db()->find(‘User’, array(‘where’=>”uname=’david'”, ‘limit’=>1) );
09.
10.
//using prepared statement to avoid sql injection
11.
Doo::db()->find(‘User’, array(
12.
‘where’ => ‘user=?’,
13.
‘param’ => array($_GET[‘name’]),
14.
‘limit’ => 1
15.
)
16.
);
17.
18.
//Or simply use this for shorter code.
19.
Doo::loadModel(‘User’);
20.
$u = new User;
21.
$u->uname = $_GET[‘name’];
22.
$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: