Aug 21

Cakephp 学习日志 (二) 不指定

bkkkd , 07:54 , 开发应用 , 评论(0) , 引用(0) , 阅读(39482) , Via 本站原创 | |

$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  
        )  
)  
内文分页: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
Tags:
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]