注意,这篇文章是针对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
这篇文章假定你已经了解了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