本文实例讲述了CakePHP框架Model关联对象。分享给大家供大家参考,具体如下:
CakePHP 提供关联数据表间的映射,共有4种类型的关联:
hasOne
,hasMany
,belongTo
,hasAndBelongsToMany
.
设定了Model间的关联关系定义,CakePHP就会将基于关系数据库的数据映射为基于对象的关系模型。
但是你应该确保遵循CakePHP的命名规则.
命名规则中需要考虑的3个内容是,外键,model名字,表名.
外键:单数形式的 modelName_id
表名:复数形式的 model名
Model名:驼峰法命名单数形式(见文件inflector.php).
hasOne 关联的定义与查询:通过在model中增加一个array来实现.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class User extends AppModel
{
var $name = 'User' ;
var $hasOne = array (
'UserInfos' => array (
'className' => 'UserInfos' ,
'conditions' => '' ,
'order' => '' ,
'dependent' => true,
'foreignKey' => 'user_id'
)
);
}
|
$hasOne 变量是一个array,CakePHP 通过该变量来构建 Blog 与 User 之间的关联。
className: 关联对象的类名。
conditions: 关联对象的选择条件。
order: 关联对象的排列方式。
dependent: 这是个布尔值,如果为 true,父对象删除时会级联删除关联子对象。
foreignKey: 指向关联 Model 的外键字段名,仅在不遵循 Cake 的命名约定时需要设置。
belongsTo 关联的定义与使用
1
2
3
4
5
6
7
8
9
10
11
12
|
class Blog extends AppModel
{
var $name = 'Blog' ;
var $belongsTo = array (
'User' => array (
'className' => 'User' ,
'conditions' => '' ,
'order' => '' ,
'foreignKey' => 'user_id'
)
);
}
|
className: 关联对象的类名。
conditions: SQL 条件子句以限定关联的对象。
order: 关联对象的排序子句。
foreignKey: 关联对象所对应的外键字段名。
hasMany 关联的定义与查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class User extends AppModel
{
var $name = 'User' ;
var $hasMany = array (
'Blog' => array (
'className' => 'Blog' ,
'conditions' => 'Blog.status = 1' ,
'order' => 'Blog.created DESC' ,
'limit' => '5' ,
'foreignKey' => 'user_id' ,
'dependent' => true,
'exclusive' => false, 'finderQuery' => ''
)
);
}
|
$hasMany array 用来定义 User 包含多条 Blog 这样的关联关系。
className: 关联对象类名。
conditions: 关联对象限定条件。
order: 关联对象排列子句。
limit: 用 limit 来限定检索的关联对象数量。
foreignKey: 外键字段名。
dependent: 是否级联删除。
exclusive: 如果为 TRUE,所有的关联对象将在一句 SQL 中删除,model 的 beforeDelete 回调函数不会被执行。
finderQuery: 定义一句完整的 SQL 语句来检索关联对象,能够对关联规则进行最大程度上的控制。
同样可以为 Blog 加上关联 User 对象的 belongTo 关联。
hasAndBelongsToMany 关联的定义与查询。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Blog extends AppModel
{
var $name = 'Blog' ;
var $hasAndBelongsToMany = array ( 'Tag' =>
array ( 'className' => 'Tag' ,
'joinTable' => 'blogs_tags' ,
'foreignKey' => 'blog_id' ,
'associationForeignKey' => 'tag_id' ,
'conditions' => '' ,
'order' => '' ,
'limit' => '' ,
'uniq' => true,
'finderQuery' => '' ,
'deleteQuery' => '' ,
)
);
}
|
$hasAndBelongsToMany array 是定义 HABTM 关联的变量。
className: 关联对象类名。
joinTable: 如果没有遵循 Cake 的命名约定建立关联表,则需要设置该 key 来指定关联表。
foreignKey: 定义本 mode 在关联表中的外键字段。
associationForeignKey: 关联表中指向关联对象的外键字段名。
conditions: 关联对象限定条件。
order: 关联对象排序子句。
limit: 关联对象数量限制。
uniq: 设为 true 的话,重复的关联对象将被过滤掉。
finderQuery: 完整的关联对象检索语句。
deleteQuery: 完整的删除关联关系的SQL 语句。
保存关联对象:
当关联的两个对象都没有持久化,你需要首先持久化主对象。
在保存子对象时要把父对象的 ID 保持在子对象中。
保存 hasAndBelongsToMany 关联对象:
使用 bindModel()
和 unbindModel()
实时地改变关联关系:
希望本文所述对大家PHP程序设计有所帮助。
相关文章
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 32
-
2025-05-25 53
-
2025-06-04 49
-
2025-05-27 97
-
2025-05-27 49