原文:5-关联模型
1、 SectionModel.class.php
要继承 RelationModel,映射模型和类名写对应上数据表和action类
1 <?php 2 class SectionModel extends RelationModel{ 3 public $_link=array( 4 ‘Category‘=>array( 5 ‘mapping_type‘=>HAS_MANY, //关联关系 1:n 6 ‘class_name‘=>‘Category‘, //对应类名 7 ‘foreign_key‘=>‘sectionid‘, //外键,分类表的字段 8 ‘mapping_name‘=>‘category‘, //映射模型 9 ‘mapping_order‘=>‘sectionid desc‘, //映射排序 10 ), 11 ‘Article‘=>array( 12 ‘mapping_type‘=>HAS_MANY, 13 ‘class_name‘=>‘Article‘, 14 ‘foreign_key‘=>‘sectionid‘, 15 ‘mapping_name‘=>‘article‘, 16 17 ), 18 ); 19
2、SectionAction.class.php
开启关联relation(true)->
<?php class SectionAction extends CommonAction{ public function index(){ $sec=new SectionModel(); //Relation(true)会关联保存User模型定义的所有关联数据 //-----查询 //dump($sec->relation(true)->find(3)); /*----创建数据 2.INSERT INTO `joys_section` (`title`) VALUES (‘自定义单元‘) [ RunTime:0.001198s ] 3.SHOW COLUMNS FROM `joys_category` [ RunTime:0.005667s ] 4.INSERT INTO `joys_category` (`title`,`alias`,`sectionid`) VALUES (‘自定义分类1‘,‘test11‘,8) [ RunTime:0.000496s ] 5.INSERT INTO `joys_category` (`title`,`alias`,`sectionid`) VALUES (‘自定义分类12‘,‘test21‘,8) [ RunTime:0.000302s ] $data[‘title‘]="自定义单元"; $data[‘category‘]=array( array(‘title‘=>‘自定义分类1‘,‘alias‘=>‘test11‘), array(‘title‘=>‘自定义分类12‘,‘alias‘=>‘test21‘) ); $sec->relation(true)->add($data);*/ /*----更新数据 /*SHOW COLUMNS FROM `joys_section` [ RunTime:0.007477s ] UPDATE `joys_section` SET `id`=8,`title`=‘自定义单元‘ WHERE ( id=8 ) [ RunTime:0.002553s ] SHOW COLUMNS FROM `joys_category` [ RunTime:0.008511s ] INSERT INTO `joys_category` (`title`,`alias`,`sectionid`) VALUES (‘自定义分类88‘,‘test‘,8) [ RunTime:0.000866s ] SHOW COLUMNS FROM `joys_article` [ RunTime:0.011497s ] $data[‘id‘]=8; //更新的时候要加上$data[‘id‘]=8;,不然从表更新不了 $data[‘title‘]="自定义单元"; $data[‘category‘]=array( array(‘title‘=>‘自定义分类88‘,‘alias‘=>‘test‘) ); $sec->relation(true)->where(‘id=8‘)->save($data); * */ //---------删除 /*DELETE FROM `joys_section` WHERE ( `id` = 8 ) [ RunTime:0.002513s ] SHOW COLUMNS FROM `joys_category` [ RunTime:0.013709s ] DELETE FROM `joys_category` WHERE ( `sectionid` = 8 ) [ RunTime:0.000967s ] SHOW COLUMNS FROM `joys_article` [ RunTime:0.014401s ] DELETE FROM `joys_article` WHERE ( `sectionid` = 8 ) [ RunTime:0.001064s ] $sec->relation(true)->delete(8);*/ $data[‘id‘]=8; //更新的时候要加上$data[‘id‘]=8;,不然从表更新不了 $data[‘title‘]="自定义单元"; $data[‘category‘]=array( array(‘title‘=>‘自定义分类88‘,‘alias‘=>‘test‘) ); $sec->relation(true)->where(‘id=8‘)->save($data); $this->display(); }
时间: 2024-10-20 22:58:30