下面两个是关联表:test_user表和test_profile表,关联字段为:test_profile表中的user_id字段关联test_user表中的id字段
模型:User.php中的代码如下:
hasOne('Profile','user_id');
}
}
模型:Pofile.php中的代码如下:
belongsTo('User','user_id');
}
}
控制器index.php中的代码如下:
'及时雨'])->select();
//通过test_user表中用户名是"李师师"这个条件,反过来查询test_profile表中符合条件的数据
$pro = Profile::hasWhere('returnUser', ['username' => '李师师'])->select();
//通过查询test_user表中id是7的记录,获取该条记录的用户昵称
$pro2 = User::get(7);
$nickname = $pro2->profile->nickname;//切记这里的$pro2->profile可千万不要写成了:$pro2->profile()。不能加括号,因为模型关联的时候,他相当于把profile当做了类的属性,虽然看上去是模型中的方法,但是他是按照获取属性的方式获取的
echo '';
print_r($user);
print_r($pro);
print_r($nickname);
}
public function index2()
{
//第一种:预载入查询
$users = User::with('profile')->select();
foreach ($users as $user) {
echo $user->profile->nickname . '';
}
/*
产生的sql语句
SELECT * FROM `test_user`
SELECT * FROM `test_profile` WHERE `user_id` IN (1,2,3,4,5,6,7,8)
*/
//如果要对关联模型进行约束,可以使用闭包的方式。
$users = User::with(['profile' => function ($query) {
$query->where('nickname', '及时雨')->whereOr('nickname', '行者');
}])->select();
foreach ($users as $user) {
echo $user->profile->nickname;
}
/**
产生的sql语句
SELECT * FROM `test_user`
SELECT * FROM `test_profile` WHERE `nickname` = '及时雨' AND `user_id` IN (1,2,3,4,5,6,7,8) OR `nickname` = '行者'
*/
//第二种:
$users = User::select();
foreach ($users as $user) {
echo $user->profile->nickname . '';
}
/*
产生的sql语句
SELECT * FROM `test_user`
SELECT * FROM `test_profile` WHERE `user_id` = 1 LIMIT 1
SELECT * FROM `test_profile` WHERE `user_id` = 2 LIMIT 1
SELECT * FROM `test_profile` WHERE `user_id` = 3 LIMIT 1
SELECT * FROM `test_profile` WHERE `user_id` = 4 LIMIT 1
SELECT * FROM `test_profile` WHERE `user_id` = 5 LIMIT 1
SELECT * FROM `test_profile` WHERE `user_id` = 6 LIMIT 1
SELECT * FROM `test_profile` WHERE `user_id` = 7 LIMIT 1
SELECT * FROM `test_profile` WHERE `user_id` = 8 LIMIT 1
*/
}
public function index3()
{
//第一种:预载入查询
$users = User::with('profile')->select();
foreach ($users as $user) {
echo $user->profile->nickname . '';
}
/*
如果User.php中 return $this->hasOne('Profile','user_id')->setEagerlyType(0);
或者 如果User.php中 return $this->hasOne('Profile','user_id');
产生的sql语句
SELECT * FROM `test_user`
SELECT * FROM `test_profile` WHERE `user_id` IN (1,2,3,4,5,6,7,8)
*/
/*
如果User.php中return $this->hasOne('Profile','user_id')->setEagerlyType(0);
产生的sql语句
SELECT `user`.`id`,`user`.`user_group_id`,`user`.`username`,`user`.`password`,`profile`.`id` AS `profile__id`,`profile`.`user_id` AS `profile__user_id`,`profile`.`nickname` AS `profile__nickname`,`profile`.`sex` AS `profile__sex` FROM `test_user` `user` INNER JOIN `test_profile` `profile` ON `user`.`id`=`profile`.`user_id`
*/
//===================================================================//
//使用withJoin()方法,就是用'join'方式,这
$users = User::withJoin('profile')->select();
foreach ($users as $user) {
echo $user->profile->nickname . '';
}
/**
SELECT `user`.`id`,`user`.`user_group_id`,`user`.`username`,`user`.`password`,`profile`.`id` AS `profile__id`,`profile`.`user_id` AS `profile__user_id`,`profile`.`nickname` AS `profile__nickname`,`profile`.`sex` AS `profile__sex` FROM `test_user` `user` INNER JOIN `test_profile` `profile` ON `user`.`id`=`profile`.`user_id`
*/
$users = User::withJoin('profile','LEFT')->select();
foreach ($users as $user) {
echo $user->profile->nickname . '';
}
/**
SELECT `user`.`id`,`user`.`user_group_id`,`user`.`username`,`user`.`password`,`profile`.`id` AS `profile__id`,`profile`.`user_id` AS `profile__user_id`,`profile`.`nickname` AS `profile__nickname`,`profile`.`sex` AS `profile__sex` FROM `test_user` `user` LEFT JOIN `test_profile` `profile` ON `user`.`id`=`profile`.`user_id`
*/
//===================================================================//
//$res = User::get(8)->profile->save(['nickname'=>'孝义','sex'=>'男']);//编辑操作
$res = User::get(8)->profile()->save(['nickname'=>'孝义','sex'=>'男']);//新增操作
var_dump($res);die;
//===================================================================//
//反过来,根据关联模型test_profile表的信息,获取test_user表的的对应的信息
return Profile::get(7)->returnUser->username;
//===================================================================//
//模型绑定:就是在输出父模型数据表中的字段时,同时还输出绑定到父模型中的关联模型中的字段
//如下:因为User.php中, return $this->hasOne('Profile','user_id')->bind('nickname');把'nickname'字段绑定到了父模型下,所以在输出父模型test_user表中的字段时,同时还会输出绑定到该模型下在test_profile表中的nickname字段
$user = User::get(1, 'profile');
echo '';
print_r($user);
echo $user->nickname;
die;
}