5-関連モデル
7080 ワード
1、 SectionModel.class.php
RelationModelを継承するには、モデルとクラス名をマッピングして、対応するデータテーブルとactionクラスを書きます.
2、SectionAction.class.php
関連付けrelation(true)をオンにします->
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();
}