Thule - 0.9.6
  • Welcome
  • Introduction
  • - MVC(Model-View-Controller)
    - Architecture of Framework
    - Flow Chart of Application
    - Services In One Server
    - Developing Guide
    - Features
  • Installation
  • Getting Started
  • - Direct Access HTTP
    - Router Access HTTP
    - Router Direct Access HTTP
    - Reserved Function
    - Convention
    - Bootstrap
    - Pack Javascript , CSS
  • Basic Modules
  • - Model
    - View
    - Controller
  • Libraries
  • - Router
    - Config
    - Response
    - Request
    - Database
    - Query
    - Cache
    - Debug
    - Log
    - Unit Test
    - Registry
    - Profile
  • Helper
  • - Auth
    - Calendar
    - Date
    - Error page
    - Email
    - Form
    - GeoIP
    - Google
    - Html Element
    - Html
    - Http
    - I18n
    - Image
    - Json
    - Jquery
    - Login
    - Layout
    - Locale
    - PHP to Javascript
    - Paging
    - Payflow
    - Soap
    - Text
    - Table
    - Uri
    - Upload
    - Validation
  • Plugins
  • - Smarty Template Engine
  • History
  • Home > Model

    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 Directory

    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);
          }
    }

    Load Model Class in Controller

                  $hconn=&$this->opendb('default');
                  $this->loadModel("sample",$hconn);
                  $this->Sample->select_member($arr_sql);

    Create Model class and get query from xml

    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>
    ............

    "sql_key" is a key of query which you want to get.

    $arrSql['sql_key']='thule';

    If you use like above, you can get the query from below query

        <query key='thule'> select * from db_thule.tb_member where member_id='$member_id' </query>

    Static Query

    'query_values' parameter passed by $arr_sql is "preserved parameter" for static query.

    $arr_sql['query_values']=array(1,2,3);
    $result=$this->Sample->select_static_member($arr_sql);

    If you use like above, you can get the query from below query

        <query key='thule'> select * from db_thule.tb_member where a='?' and b='?' and c='?' </query>

    Get generated to be below

    select * from db_thule.tb_member where a='1' and b='2' and c='3'