💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 关联查询 **HAS_ONE** 每篇文章包含一个发布者,即为 HAS_ONE, 查询结果是一维数组。 *模型* ~~~ namespace Home\Model; class UserModel extends \Took\Model\RelationModel { public $table = 'user'; public $relation = array( 'user_info' => array(// 关联表 'type' => HAS_ONE, // 包含一条主表记录 'foreign_key' => 'uid', //user_info 表字段 'parent_key' => 'uid', //user 表字段 'field' => array('address' => 'dizi'), // 关联表检索的字段 ) ); } ~~~ *控制器* ~~~ namespace Home\Controller; class IndexController extends \Took\Controller { function index() { $db = K('user'); $row = $db->all(); } } ~~~ **HAS_MANY** *模型* ~~~ namespace Home\Model; class UserModel extends \Took\Model\RelationModel { public $table = 'user'; public $relation = array( 'news' => array(// 关联表 'type' => HAS_MANY, // 包含一条主表记录 'foreign_key' => 'uid', //user_info 表关联字段 'parent_key' => 'uid', //user 表字段 ), ); } ~~~ **BELONGS_TO** *模型* ~~~ namespace Home\Model; class NewsModel extends \Took\Model\RelationModel { public $table = 'news'; public $relation = array( 'category' => array(// 关联表 'type' => BELONGS_TO, //category 被 news 关联 'foreign_key' => 'cid', // 从表字段 (category 表 ) 'parent_key' => 'cid', // 主表字段 (news 表 ) ) ); } ~~~ **MANY_TO_MANY** 一般情况下将多对多通过中间表转化为多个一对多关系,中间表的关联字段必须与关联的两张表主键字段名相同。 *模型* ~~~ namespace Home\Model; class UserModel extends \Took\Model\RelationModel { public $table = 'user'; public $relation = array( 'role' => array(// 关联表 'type' => MANY_TO_MANY, // 多对多关系 'relation_table' => 'user_role', // 中间表 'foreign_key' => 'rid',//role 表字段 'parent_key' => 'uid',//user 表字段 ) ); } ~~~ ### 关联添加 TookPHP框架可以快速完成关联数据的添加操作,程序员所要做的工作就是配置好关联操作模型参数即可,可以说是一劳永逸的事情 **HAS_ONE** *模型* ~~~ namespace Home\Model; class UserModel extends \Took\Model\RelationModel { public $table = 'user'; public $relation = array( 'user_info' => array( // 关联表 'type' => HAS_ONE, // 包含一条主表记录 'parent_key' => 'uid', //user 表主键 'foreign_key' => 'uid', //user_info 表关联字段 ) ); } ~~~ *控制器* ~~~ $db = K('User'); $data = array( 'username' => '张三', 'user_info' => array( 'email' => 'test@qq.com', 'address'=>'上海' ), ); $db->insert($data); ~~~ **HAS_MANY** 设置与 HAS_ONE 相同,只需要修改 type 值 **BELONGS_TO** 设置与 HAS_ONE 相同,只需要修改 type 值 **MANY_TO_MANY** 设置与 HAS_ONE 相同,只需要修改 type 值 ## 关联更新 **HAS_ONE** 从表可以没有主键字段 *模型* ~~~ namespace Home\Model; class UserModel extends \Took\Model\RelationModel { public $table = 'user'; public $relation = array( 'user_info' => array(// 关联表 'type' => HAS_ONE, // 包含一条主表记录 'foreign_key' => 'uid', //user_info 表字段 'parent_key' => 'uid', //user 表字段 ) ); } ~~~ *控制器* ~~~ $db = K('User'); $data = array( 'uid' => 1, 'username' => 'admin', 'user_info' => array( 'address' => 'test@163.com' ) ); $db->save($data); ~~~ **HAS_MANY** 与 HAS_ONE 使用相同 , 从表必须有主键字段 **BELONGS_TO** 与 HAS_ONE 使用相同 , 从表必须有主键字段 **MANY_TO_MANY** 与 HAS_ONE 方法相同 , 从表必须有主键字段 ### 关联删除 **HAS_ONE** *模型* ~~~ namespace Home\Model; class UserModel extends \Took\Model\RelationModel { public $table = 'user'; public $relation = array( 'user_info' => array(// 关联表 'type' => HAS_ONE, // 包含一条主表记录 'foreign_key' => 'uid', //user_info 表字段 'parent_key' => 'uid', //user 表字段 ) ); } ~~~ *控制器* ~~~ $db = K('User'); $status = $db->del(1); ~~~ **HAS_MANY** 使用方法与 HAS_ONE 一样 **BELONGS_TO** 为了数据完整性 , 只删除主表数据,从表不删除 **MANY_TO_MANY** 使用方法与 HAS_ONE 一样