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 extending the DooModel, we now can do the same operation with cleaner code:
//First load the User Model
Doo::loadModel('User');
$user = new User;
//find all users
$result = $user->find();
//count total users
$result = $user->count();
//limit 10 results and order by username ascendingly
$result = $user->limit(10, 'uname');
//find one User only, an object is returned if found.
$result = $user->getOne();
//find david
$user->uname = 'david';
$result = $user->getOne();
We have more features with the DooModel such as dynamic querying:
//find user id 15
$user->getById(5);
$user->getByGroup('moderators');
//find user by group and vip status(TINYINT)
$user->getByGroup_vip('normal', 1);
//And you can limit only 1 result, result will be as an Object
$user->getById_first(5);
$user->getByGroup_first('moderators');
$user->getByGroup_vip_first('normal', 1);
You can find more information on DooModel in the API doc at http://doophp.com/doc/api


ai
25 Sep, 2009
如何扩展model业务逻辑呢?
mhdex
27 Sep, 2009
扩展model业务逻辑? 可以extends DooModel 呀
class MyModel extends DooModel{ public function test(){} } Doo::loadModel('MyModel'); class User extends MyModel{ } $user = new User; $user->test();Nay
7 Oct, 2009
Hi Leng,
Can you write a post about showing all users (user list)?
I would like to know a way to loop an user array and print out in view.
Thanks.
Nay
7 Oct, 2009
And also, is there a debug mode in DooPhp?
I would like to know what are the data inside a variable before posting to view.
Something like what we can do in cake.
debug($users);die;
Thanks
Leng
7 Oct, 2009
sure, will write a simple post on this.
Roman
11 Oct, 2009
Leng, if you do $result = $user->find(); $result = $user->count(), will the second method call access the DB again or be aware of the previous action and use its result?
Leng
11 Oct, 2009
no it won’t. It only return the result. However if you had
$user->id = 10;It will search based on the options.Roman
12 Oct, 2009
I am not clear on that. “no, it won’t” access the DB again or won’t be aware of the previous action?
Leng
13 Oct, 2009
it won’t be aware of the previous query action
cos800
5 Jan, 2010
应该要有个getVal
Leng
6 Jan, 2010
getVal 是什么用途?
Surt
3 Mar, 2010
Hi Leng,
i saw the Doo::db()->toArray() is there any way for an object extending DooModel to transform it to Array directly?
And of course congratulations for dooPHP, really great framework, and the pluses, route generation, etc.. really good work
Surt
3 Mar, 2010
Another question is:
i can’t set this relationship with the DooModel actually provided due to error with database alias:
Content has many Content
al i want is to set a tree, with the table content
[Content table]
pk
parent_fk
content.parent_fk points to content.pk, the aliases for the SELECT fields are right, but not the database ones.
cevarief
11 Mar, 2010
@Surt, to convert directly to array is by using ‘asArray’ to parameter option.
$result = Doo::db()->find(’User’,array(’asArray’=>TRUE);
Leng, is it possible to make ‘asArray’ as a methode ? it think it will look nicer.
$result = Doo::db()->find(’User’)->asArray();
Patrick
8 Aug, 2010
I get an error:
Fatal error: Class MyAppDbUpdater contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (DooDbUpdater::getCurrentDbVersion, DooDbUpdater::storeCurrentDbVersion)
Patrick
8 Aug, 2010
Oh sorry – please delete that comment – wrong page!!
Bill
23 Sep, 2010
Where are all the options documented for find()? I did not see complete documentation in the API reference.