Model
You can deal with database easily by using Model.
Create Model
Minimum Model is as follows. Create file named 「table name.php」.
Class name is ucfirst( strtolower( table name ) ).
| user.php |
<?php
class CUser extends CModel
{
}
?>
|
Regist Model
You can regist model in function 'config_models' called by framework.
| config.php |
function config_database( &$db )
{
$db->add( "", "localhost", "user", "password", "dbname" );
$db->add( "special", "192.168.0.100", "user", "password", "dbname" );
$db->add( "pg", "localhost", "user", "password", "dbname", DBKIND_PGSQL, '5432' );
}
|
function config_models( &$controller )
{
$controller->AddModel( "user.php" );
$controller->AddModel( "specialuser.php", "special" );
$controller->AddModel( "test.php", "pg" );
}
|
How to use
You can call Model from Controller. Way to call is next two ways. First way
is set up when the name is not competed with other variable name.
$c->user->function();
$c->m["user"]->function();
|
Function
- array find( [ string condition [, string order [, string limit [, string
group ]]] )
You get elements specified by argument from table.
$results = $c->user->find( "id=$id", "age DESC" );
|
- array findone( [ string condition [, string order ]] )
You get first element from elements found by function 'find'.
$result = $c->user->findone( "id=$id", "age DESC" );
|
- array findquery( string query [, string condition [, string order [, string
group ] )
Use this when you want to find related data. 'query' is message before 'WHERE'.
$query = "SELECT user.*, office.name FROM user"
. " LEFT JOIN user.office_id=office.id";
$results = $c->user->findquery( $query, "age=24", "age DESC" );
|
- int getcount( [ string condition [, string limit ]] )
You get elements count found.
$count = $c->user->getcount( "id=$id", "10" );
|
- bool insert( array datas )
Insert data to table by array. Array is consisted as follows ( $array[key]
= value ).
$data["name"] = $name;
$data["email"] = $email;
$c->user->insert( $data );
|
- bool update( array datas )
You can update data by array. Array is same as one of 'insert'. But specify
key 'id' ( which is default key. You can specify id name by Model's member variable
$id. ). Update condition will become "id='$id'". Framework terminate
function if id key is not exist in array.
$data["id"] = $id;
$data["name"] = $name;
$data["email"] = $email;
$c->user->update( $data );
|
- bool updateby( array datas, string condition )
You can specify condition when update.
$data["name"] = $name;
$data["email"] = $email;
$c->user->updateby( $data, "age=25" );
|
- bool del( string condition )
Delete record by condition.
$c->user->del( "age=25" );
|
- result query( string query )
You can specify query.
Get last inserted ID. ( only mysql )
Get affected rows num.
Get last error.
- string to_datetime( int time )
Change UNIX timestamp to DATETIME format.
(ex.:2006-09-19 12:24:46)
- string escape( string str )
Escape string for query.
- bool validate( array datas )
- string validatemsg( array datas )
- array GetValidateError()
These are function for validation. See manual of validation to know detail.
Variable
$id( default is 'id' ) is a primary key used by function 'update' etc.
Model use database connection specifid by $name.
You can specify table name when you don't want to default table name.
|