Skip to content

Commit 7aed8ea

Browse files
committed
chapter-5 ok
1 parent 73dd30c commit 7aed8ea

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

chapter-5/Application/Home/Controller/IndexController.class.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Home\Controller;
33

4+
use Home\Model\PostModel;
5+
use Home\Model\PostViewModel;
46
use Home\Model\UserModel;
57
use Think\Controller;
68
use Think\Model;
@@ -46,4 +48,32 @@ public function update()
4648
print_r($user->find(6));
4749
}
4850
}
51+
52+
public function posts()
53+
{
54+
$m = new PostViewModel();
55+
$data = $m->select();
56+
print_r($data);
57+
}
58+
59+
public function posts2()
60+
{
61+
$m = new UserModel();
62+
$data = $m->relation('extra')->find();
63+
print_r($data);
64+
}
65+
66+
public function posts3()
67+
{
68+
$m = new PostModel();
69+
$data = $m->relation('author')->find();
70+
print_r($data);
71+
}
72+
73+
public function posts4()
74+
{
75+
$m = new UserModel();
76+
$data = $m->relation('posts')->find();
77+
print_r($data);
78+
}
4979
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: xialei
5+
* Date: 2016/4/7 0007
6+
* Time: 15:38
7+
*/
8+
namespace Home\Model;
9+
10+
use Think\Model\RelationModel;
11+
12+
class PostModel extends RelationModel
13+
{
14+
public $_link = array(
15+
'author' => array(
16+
'mapping_type' => self::BELONGS_TO,
17+
'class_name' => 'User',
18+
'foreign_key' => 'user_id',
19+
'mapping_fields' => 'username,created_at'
20+
)
21+
);
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: xialei
5+
* Date: 2016/4/7 0007
6+
* Time: 15:08
7+
*/
8+
namespace Home\Model;
9+
10+
use Think\Model\ViewModel;
11+
12+
class PostViewModel extends ViewModel
13+
{
14+
public $viewFields = array(
15+
'Post' => array('post_id', 'title', 'content', 'created_at', 'updated_at'),
16+
'User' => array('username' => 'author', '_on' => 'Post.user_id=User.id')
17+
);
18+
}

chapter-5/Application/Home/Model/UserModel.class.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* User: xialeistudio<1065890063@qq.com>
99
* Date: 2016-03-24
1010
*/
11-
class UserModel extends Model
11+
class UserModel extends Model\RelationModel
1212
{
1313
private $denyUsernames = array(
1414
'admin',
@@ -27,7 +27,22 @@ class UserModel extends Model
2727
public $_auto = array(
2828
array('password', 'md5', self::MODEL_BOTH, 'function'),//新增或编辑的时候使用md5函数处理密码
2929
array('created_at', 'time', self::MODEL_INSERT, 'function'),//新增的时候将创建时间设为当前时间戳
30-
array('updated_at','time',self::MODEL_UPDATE,'function'),//更新的时候将更新时间设为当前时间戳
30+
array('updated_at', 'time', self::MODEL_UPDATE, 'function'),//更新的时候将更新时间设为当前时间戳
31+
);
32+
33+
34+
public $_link = array(
35+
'extra' => array(
36+
'mapping_type' => self::HAS_ONE,
37+
'class_name' => 'UserExtra',
38+
'foreign_key' => 'user_id',
39+
'mapping_fields' => 'email,qq',
40+
),
41+
'posts' => array(
42+
'mapping_type' => self::HAS_MANY,
43+
'class_name' => 'Post',
44+
'foreign_key' => 'user_id'
45+
)
3146
);
3247

3348
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy