ThinkPHP5有关联模型的操作
1 2 3 4 5 6 7 8
| hasOne:有一个,加上主谓语应该是 ,A 有一个 B hasMany:有很多,A 有很多 B belongsTo:属于, A 属于 B belongsToMany:多对多
一对一:HAS_ONE 以及对应的BELONEGS_TO 一对多:HAS_MANY 以及相对的BELONGS_TO 多对多:BELONGS_TO_MANY
|
确立关联模型
在关系型数据库中,表之间有一对一、一对多、多对多的关系。在 TP5 中,实现了ORM (Object Relational Mapping) 的思想,通过在模型中建立模型间的关联,实现建立表与表之间的关联
建立E-R图
一对一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class User extends Model{ public function profile(){ return $this->hasOne('Profile','user_id','id'); } }
class Profile extends Model{ public function user(){ return $this->belongsTo('User'); } }
public function add(){ $user = new UserModel; $user->name = 'thinkphp'; $user->password = '123456'; $user->nickname = '宝宝不舒服'; if($user->save()){ $profile['truename'] = '宝宝不舒服'; $profile['birthday'] = '1989-03-05'; $profile['address'] = '深圳'; $user->profile()->save($profile); return $this->success('添加成功'); }else{ return $user->getError(); } }
public function read($id){ $user = UserModel::get($id); if($user){ echo $user->name; echo $user->nickname; echo $user->profile->truename; } } public function update($id){ $user = UserModel::get($id); $user->name = 'framework'; if($user->save()){ $user->profile->email = '609392790@qq.com'; $user->profile->save();
}else{ return $user->getError(); }
} public function delete($id){ $user = UserModel::get($id); if($user->delete()){ $user->profile->delete(); }else{ return $user->getError(); } }
|
一对多
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| class User extends Model{ public function books(){ return $this->hasMany('Book'); }
} class Book extends Model{ public function user(){ return $this->belongsTo('User'); } }
public function addBook(){ $user = UserModel::get(1); $book = new Book; $book->title = 'thinkphp'; $book->publih_time = '2016-05-06'; $user->books()->save($book); } public function batchAddBook(){ $user = UserModel::get(1); $book = [ ['title'=>'thinkphp5.0','publish'=>'2016-05-06'], ['title'=>'thinkphp3.2','publish'=>'2014-05-06'] ]; $user->books()->saveAll($books); } public function read(){ $user = UserModel::get(1); $books = $user->books()->where('status',1)->select(); $book = $user->books()->getByTitle('宝宝想哭'); } public function read(){ $user = UserModel::has('books')->select(); $user = UserModel::has('books','>=',3)->select(); $user = UserModel::hasWhere('books',['title'=>'宝宝不哭'])->select(); } public function update($id){ $user = UserModel::get($id); $book = $user->books()->where('title','宝宝不哭')->update(['title'=>'宝宝就要哭']); } public function delete($id){ $user = UserModel::get($id); $book = $user->books()->getByTitle('Thinkphp'); $book->delete(); }
|
多对多
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public function roles(){ return $this->belongsToMany('Role','Think_access'); }
public function user(){ return $this->belongsToMany('User','think_access'); }
public function add(){ $user = UserModel::getByNickname('张三'); $user->roles()->save(['name'=>'editor','title'=>'编辑']); } public function add(){ $user = UserModel::getByNickname('张三'); $user->roles->saveAll([ ['name'=>'leader','title'=>'领导'], ['name'=>'admin','title'=>'管理员'], ]); }
public function delete(){ $usr = UserModel::get(2) ; $role = Role::getByName('admin'); $user->roles()->detach($role); }
|
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class Category extends Model { public function product(){ return $this->hasMany('product','category_id','id'); } } class Product extends Model { public function property(){ return $this->hasMany('property','goods_id','id'); } }
public function index() { return Category::with('product,product.property')->where('id',1)->find(); } class Category extends Model { public function product(){ return $this->hasMany('product','category_id','id'); }
public function list(){ return self::with([ 'product'=>function($query){ $query->with('property')->field('name')->order('price'); } ])->select(); } }
|
注意
1 2 3 4 5 6 7 8 9
| $model->with(['adminUsers'=>function($query){ $query->where("type",$this->param['type']); }]);
$model->alias('a') ->join('admin_users u','a.admin_id = u.id','LEFT') ->where('u.type',$this->param['type']);
|