用户
 找回密码
 入住 CI 中国社区
搜索
查看: 12207|回复: 4
收起左侧

[中级] 从头学CodeIgniter和Doctrine系列 第二天 基础知识【翻译】

  [复制链接]
发表于 2010-1-21 08:33:51 | 显示全部楼层 |阅读模式
本帖最后由 baiyuxiong 于 2010-1-21 08:35 编辑

英文原文:http://www.phpandstuff.com/articles/codeigniter-and-doctrine-from-scratch-day-2-the-basics
中文原文:http://www.baiyuxiong.com/article/122.htm

从头学CodeIgniter和Doctrine第二天 基础知识

在这一部分:
  • 快速回顾一下CodeIgniter和Doctrine的安装
  • 简单复习CodeIgniter和Doctrine的一些基本概念
  • 首先我们将回顾CodeIgniter中的Controllers和Views。
  • 然后是使用Doctrine的Models,这是与其它CodeIgniter教程的不同之处。
  • 最后,我们预览一个Doctrine Model的例子并讨论我们的项目将会是什么样的。

开始前请确认已全新安装了CodeIgniter+Doctrine。

快速安装CodeIgniter+Doctrine

全新安装说明:
(如果你想使用第一天安装好的文件,跳到下一节)

如果你有第一天的文件
(如果你使用上一节的全新安装,跳过这节)
  • 重命名或复制安装文件夹到ci_doctrine目录(这么做的话,以后不需要每次教程都改变site url)
  • 删除:system/application/models/user.php
  • 删除:system/application/controllers/hello.php
  • 删除表:user
  • 编辑:system/application/config/config.php


PHP复制代码
 
// in system/application/config/config.php
$config['base_url'] = "http://localhost/ci_doctrine/";[/url]
 
复制代码


CodeIgniter URL 结构

CodeIgniter中的URL看起来像这样:
这个URL调用叫做“CONTROLLER_NAME”控制器类,和“METHOD_NAME”的方法
[url=http://localhost/ci_doctrine/index.php/CONTROLLER_NAME/METHOD_NAME]http://localhost/ci_doctrine/index.php/CONTROLLER_NAME/METHOD_NAME


和以前一样,除非调用一个index()的默认方法
http://localhost/ci_doctrine/index.php/CONTROLLER_NAME

这次它传送一个“VALUE”做为控制器方法的参数。
http://localhost/ci_doctrine/index.php/CONTROLLER_NAME/METHOD_NAME/VALUE

值可以是数据或字符串,你可以在URL中继续增加更多的值来传递额外的参数。

了解更多信息:
传送uri参数http://codeigniter.com/user_guide/general/controllers.html#passinguri
http://codeigniter.org.cn/user_guide/general/controllers.html#passinguri 译者注

CodeIgniter MVC (Model – View – Controller)

Models
我们将使用Doctrine的Model, 而不是CodeIgniter的. 我会在后面的教程中解释.
如果你想了解CodeIgniter Models, 请阅读读: CodeIgniter Models
英文:http://codeigniter.com/user_guide/general/models.html
中文http://codeigniter.org.cn/user_guide/general/models.html 译者注

Views
Views创建在system/application/views文件夹下,像这样命名:my_view.php。它们是输出模板,包含html,javascript和其它东西。
Views 也包含内联php代码(用来显示消息,进行循环等)。Controllers通常载入views来显示输出。
官方文档:http://codeigniter.com/user_guide/general/views.html
http://codeigniter.org.cn/user_guide/general/views.html 译者注

Controllers
我们在第一天已经讲过了,查看这一节:CodeIgniter 速成: Controllers

Controller和View
这是一个View的例子(system/application/views/my_view.php):
PHP复制代码
 
Some plain text.
<div>
    You can use HTML.
</div>
<div>
    Display variables passed from a controller: <br />
    <?php echo $message; ?> <br />
    <?php echo $another_message; ?>
</div>
 
You can even do loops: <br />
<?php for ($i = 0; $i < 3; $i++) {
    echo "Counter shows: $i <br />";
} ?>
Or in alternate syntax: <br />
<?php for ($i = 0; $i < 3; $i++): ?>
    Counter shows: <?php echo $i; ?> <br />
<?php endfor; ?>
 
复制代码


这是Controller的例子(system/application/controllers/sample_controller.php),载入一个view。
PHP复制代码
 
class Sample_controller extends Controller {
    function index() {
        $data = array();
        $data['message'] = "index was called";
        $data['another_message'] = "blah blah";
        $this->load->view('my_view',$data);
    }
}
复制代码


这两个url均有效:
http://localhost/ci_doctrine/index.php/sample_controller/index
http://localhost/ci_doctrine/index.php/sample_controller
浏览器显示:
HTML复制代码
 
Some plain text.
You can use HTML.
Display variables passed from a controller:
index was called
blah blah
You can even do loops:
Counter shows: 0
Counter shows: 1
Counter shows: 2
Or in alternate syntax:
Counter shows: 0
Counter shows: 1
Counter shows: 2
 
复制代码

注意:
  • View 包含了原始输出和简单的内联php。
  • index()是一个默认的控制器方法,所以不是必须把它写在URL上。
  • $this->load->view(’my_view’,$data)载入view并输出到浏览器。
  • 第一个参数‘my_view’是view文件的名称,不需要加'.php'。
  • 第二个参数$data是一个数组,它向View传送变量
  • 例子:在view里,$data['message']变成$message,$data['another_message']变成$another_message。
    (如果你创建了上面的文件来测试代码,你现在可以删掉了,我们的工程再不需要它们了。)

Doctrine Models
Models是代表数据(通常来自数据库)的类.
例如,你有一个user表。所以我们可以创建一个模型类叫做“User”来代表数据库表中的记录。
我们的模型类应该能够进行CURD(Create, Read, Update and Delete)操作。幸运的是,Doctrine可以用少量简洁的代码很好的帮助你完成这些工作。

使用中的差异(与CodeIgniter的模型比较)

他们继承自Doctrine_Record类(而不是“Model”类).
可以像这样载入它们:$u = new User();(而不是这样: $u = $this->load->model(’user’);) 由于我们在插件中注册的Doctrine自动载入。
php4不兼容
这是所有目前为止你需要知道的。这对于已经使用CodeIgniter的人来说。应该是一个很容易的转变。

Doctrine模型是什么样的?

这是一个我们将要建立的一种模型的预览。在下一次教程中,我们将涉及更多的细节。
PHP复制代码
 
<?php
class User extends Doctrine_Record
{
    // 这个函数定义表的字段
    public function setTableDefinition() {
        $this->hasColumn('username', 'string', 255);
        $this->hasColumn('password', 'string', 255);
        $this->hasColumn('email', 'string', 255, array(
            'email' => true // It can validate e-mail input
        ));
 
        // 支持很多字段类型, 包括enum
        $this->hasColumn('status', 'enum', null,
            array('values' => array('unverified', 'verified', 'disabled'))
        );
        $this->hasColumn('referer_id', 'integer', 4);
    }
    // 设置一些选项
    public function setUp() {
        // 与“post”模型建立关系
        $this->hasMany('Post as Posts', array(
            'local' => 'id',
            'foreign' => 'post_id'
        ));
        // 甚至可以有一个与自身的关系
        $this->hasOne('User as Referer', array(
            'local' => 'referer_id',
            'foreign' => 'id'
        ));
        // 让'created_at'和'updated_at' 字段自动更新
        $this->actAs('Timestampable');
        // 密码字段指定Mutator, 用来自动加密
        $this->hasMutator('password', 'md5Password');
    }
    // 一个 mutator函数
    public function md5Password($value) {
        $this->_set('password', md5($value));
    }
}
 
复制代码

一旦你像这样创建了一个Doctrine模型,我们可以做各种数据库操作,Doctrine甚至可以只根据model的信息来创建表。还有其它方式创建Doctrine模型,如例用schema文件。我们以后再说。

耐心等待
下一部分,我们将开始创建一个留言板工程。这是一个教程项目的最佳示例。因为模型间的各种不同的关系,比如用户, 帖子, 主题, 论坛, 群组, 附件 等等
下次见!

评分

参与人数 1威望 +5 收起 理由
Hex + 5 精品文章

查看全部评分

发表于 2010-1-21 10:50:38 | 显示全部楼层
很少见到连载文章!顶~~~
发表于 2010-1-21 12:51:32 | 显示全部楼层
,先谢一个!又学到新知识了。
发表于 2010-3-12 13:46:20 | 显示全部楼层
第一次使用ORM。学习了。
发表于 2010-7-23 09:42:52 | 显示全部楼层
继续,

本版积分规则