---
title: "[CakePHP Manual 中文手册 翻译 预览版]"
author: "kj23"
date: "2006-12-01T09:43:25Z"
tags: []
categories: ["编程语言"]
slug: "cakephp-manual"
draft: false
lastmod: "2006-12-01T09:43:25Z"
id: 36785
url: "https://kj23.iteye.com/blog/36785"
---

www.cakephp.org

CakePHP Manual 中文手册 翻译

http://www.1x3x.net/cakephp/index.html

预览地址


目前翻译工作完成 2/3 

争取本周末完成第一稿

页面样式稍微调了一下，实在太累了，先放这么点出来

希望捧个人场 小弟我是个新人，望指教

看到网上也有别的同学翻译了，唉，就当学习过程吧

PHP on rails特别是他的ajax基于prototype蛮对我胃口的

任何我的错误都希望你提出来啊，谢谢

hasOne 关联的定义与查询
假设你已经准备好了User和Profile两个model，让我们来定义他们之间的关联。hasOne关联的定义是通过在model中增加一个array来实现的。下面是示例代码


```

<?php
class User extends AppModel
{
    var $name = 'User';
    var $hasOne = array('Profile' =>
                        array('className'    => 'Profile',
                              'conditions'   => '',
                              'order'        => '',
                              'dependent'    =>  true,
                              'foreignKey'   => 'user_id'
                        )
                  );
}
?>

```



$hasOne变量是一个array，Cake通过该变量来构建User与Profile之间的关联。我们来看每一个元素代表的意义：

className (required)：关联对象的类名，上面代码中我们设为'Profile'表示关联的是Profile对象。 
conditions: 关联对象的选择条件，（译注：类似hibernate中的formula）。具体到我们的例子来看，假设我们仅关联Profile的header color为绿色的文件记录，我们可以这样定义conditions，"Profile.header_color = 'green'"。 
order: 关联对象的排序方式。假设你希望关联的对象是经过排序的，你可以为order赋值，就如同SQL中的order by子句："Profile.name ASC"。 
dependent：这是个布尔值，如果为true，父对象删除时会级联删除关联子对象。在我们的Blog中，如果"Bob"这个用户被删除了，则关联的Profile都会被删除。类似一个外键约束。 
foreignKey：指向关联Model的外键字段名。仅在你不遵循Cake的命名约定时需要设置。 
现在，现在当我们使用find() findAll()检索User对象时，你会发现关联的Profile对象也被检索回来，非常的方便：


```

$user = $this->User->read(null, '25');
print_r($user);

//output:

Array
(
    [user] => Array
        (
            [id] => 25
            [first_name] => John
            [last_name] => Anderson
            [username] => psychic
            [password] => c4k3roxx
        )

    [profile] => Array
        (
            [id] => 4
            [name] => Cool Blue
            [header_color] => aquamarine
            [user_id] = 25
        )
)

```