CakePHP框架Model关联对象用法分析

2019-11-13,,,

本文实例讲述了CakePHP框架Model关联对象。分享给大家供大家参考,具体如下:

CakePHP 提供关联数据表间的映射,共有4种类型的关联:

hasOne,hasMany,belongTo,hasAndBelongsToMany.

设定了Model间的关联关系定义,CakePHP就会将基于关系数据库的数据映射为基于对象的关系模型。

但是你应该确保遵循CakePHP的命名规则.

命名规则中需要考虑的3个内容是,外键,model名字,表名.

外键:单数形式的 modelName_id
表名:复数形式的 model名
Model名:驼峰法命名单数形式(见文件inflector.php).

hasOne 关联的定义与查询:通过在model中增加一个array来实现.

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 关联的定义与使用

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 关联的定义与查询

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 关联的定义与查询。

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框架相关内容感兴趣的读者可查看本站专题:《php优秀开发框架总结》、《codeigniter入门教程》、《ThinkPHP入门教程》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

《CakePHP框架Model关联对象用法分析.doc》

下载本文的Word格式文档,以方便收藏与打印。