Model class is help class for database handle in model.Sometimes we need the complex query and some special work after fetch
data from database.At that time Model class will be needed for that.
Model class is located in "models" directory. "Models" directory is special directory,so you have to avoid the name when you create the directory in models.
First, have a look in "Models" directory. There is sampleModel for helping your understand.
If you want to use the static query, add the "QUERY_STATIC" to "select" function in class.
class SampleModel extends Thule_Model {
function __construct(Database $hconn){
parent::__construct($hconn);
$this->arrSql['sql_section']='thule';
}
function select_member($arrSql) {
$arrSql['sql_key']='thule';
return $this->select($arrSql);
}
function static_select_member($arrSql) {
$arrSql['sql_section']='static_thule';
$arrSql['sql_key']='thule';
return $this->select($arrSql,QUERY_STATIC);
}
function static_select_member($arrSql) {
$arrSql=array(
'sql_key'=>'select_forum_board_post',
'query_values'=>array($postId));
return $this->select($arrSql,QUERY_STATIC);
}
function update_member($arrSql) {
$arrSql['sql_key']='thule_update';
$this->execute($arrSql);
}
}
$hconn=&$this->opendb('default');
$this->loadModel("sample",$hconn);
$this->Sample->select_member($arr_sql);
When you create Model class ,you should extend the Model class to your own Model class.
And you put the configure of "sql_section" to construct in your class.
You can initialise the sql-query-file, sometime you need to get the several query files for getting perfomance
and esay maintenance.
class SampleModel extends Thule_Model {
function __construct(Database $hconn){
parent::__construct($hconn);
$this->initQueryFile('sample_queries.xml');
$this->initSqlSection('thule');
$this->setDBConnect('default'); // database connection
$this->arrSql['sql_section']='thule';
}
}
"sql_section" is a "<thule>" as some kind of category in database/queries/queries.xml.
You can define the area of query scope,so it is very useful for huge scale system because of you can divide the person who should use the query and charges in a part of system to defined section of area.
...........
<thule>
......
<query key='thule'> select * from db_thule.tb_member where member_id='$member_id' </query>
.......
</thule>
<blog>
......
<query key='select_blog'> select * from tb_blog where member_id='?' </query>
.......
</blog>
<home>
......
<query key='select_home'> select * from tb_home where member_id='$member_id' </query>
.......
</home>
............
$arrSql['sql_key']='thule';
<query key='thule'> select * from db_thule.tb_member where member_id='$member_id' </query>
$arr_sql['query_values']=array(1,2,3);
$result=$this->Sample->select_static_member($arr_sql);
<query key='thule'> select * from db_thule.tb_member where a='?' and b='?' and c='?' </query>
select * from db_thule.tb_member where a='1' and b='2' and c='3'