kohana3.3 ORM

注意,這篇文章是針對kohana3.3的。因爲在《Kohana.3.0.Beginners.Guide》這本書中,介紹的3.0版本的相應的操作方法,我實驗是行不通的。

這篇文章假定你已經瞭解了kohana的目錄結構。如果還不瞭解,請自行去官網查看。

假設我們使用mysql,並且有Test數據庫,其擁有者的username爲Test,其password爲123456.我們要對其中的tests表進行操作。
注意其中的表名,起這個名字是有講究的,需要遵循下面的約定:
1.     Make table names plural (i.e.: users).
2.     Make model's filename singular (i.e.: application/classes/model/user.php). #有點扯淡,請看下面的解釋
3.     Make the model class name singular (i.e.: Model_User).
4.     Make the primary key name 'id', and set it to auto_increment.
5.      Make any pivot tables use alphabetical order (i.e.: roles_users).
其中第二條約定有點扯淡,因爲kohana官方的對於model和controller的文件的約定是(https://kohanaframework.org/3.3/guide/kohana/conventions):
   Class names should have uppercase first letters with underscores to separate words. Underscores are significant as they directly reflect the file location in the filesystem.
而第二條約定明顯違反了這個規則。而且,我按照第二條規則起的文件名,會提示找不到Model類!

下面是具體步驟:
        1.開啓kohana的ORM模塊:
            在application/bootstrap.php中,取消Kohana::modules中的ORM前面的註釋取消掉。
        2.在application/config/下新建database.php文件,其中內容如下:
              <?php defined('SYSPATH') or die('No direct access allowed');
            return array(
                'default' => array(
                    //Notice:the value fo type is Case Sensitive.
                    //Don't use 'Mysql',because it will be deprecated.
                    'type' => 'MySQLi',
                    'connection' => array(
                        'hostname' => 'localhost',
                        'database' => 'Test',
                        'username' => 'Test',
                        'password' => '123456',
                        'persistent' => FALSE,
                    ),
                    'table_prefix' => '',
                    'charset' => 'utf8',
                    'caching' => FALSE,
                    'profiling' => TRUE,
                ),
            );
        3.新建tests表:
            create table tests(
                id int auto_increment primary key,
                name varchar(50) not null,
                content varchar(50) not null
            );
            insert into tests values('A','AA');
        4.在application/classes/Model/Test.php,內容如下:
            <?php defined('SYSPATH') or die('No direct script access');

            /*
             * Don't need any thing.
             *
             * */
            class Model_Test extends ORM{}
        5.在application/classes/Controller/TestORM.php,其內容如下:
            <?php

            class Controller_TestORM extends Controller{

                public function action_index(){

                    if ($this->request->param('opt') == 'insert'){
                        $test = ORM::factory('Test');
                        $test -> name = 'B';
                        $test -> content = 'BB';
                        $test -> save();

                        $value = ORM::factory('Test')->where('name','=','B')->find()->content;
                    } else if ($this->request->param('opt') == 'query') {
                        $test = ORM::factory('Test');
                        $value = $test->where('id','=',$this->request->param('id'))->find()->content;
                    }

                    $this->response->body($value);

                }

            }

            ?>

            代碼很簡單,一看就明白。如果其中有不明白的地方,請查看官方的API文檔。
        6.爲這個測試添加路由:
            在application/bootstrap.php中,Route::set('default', '(<controller>(/<action>(/<id>)))')之前,添加如下內容:
            Route::set('TestORM','orm/<opt>(/<id>)')
                ->defaults(array(
                    'controller' => 'testORM',
                    'action' => 'index',
                ));
        7.在URL中輸入http://localhost/YourProjectName/orm/query/1應該就能查看到頁面輸出的AA

發佈了208 篇原創文章 · 獲贊 61 · 訪問量 98萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章