作者:顾久君_152_599 | 来源:互联网 | 2023-05-19 20:01
学习内容:视频教程:https:www.kancloud.cntpshopthinkphp5228987完全开发手册:https:www.kancloud.cnmanual
学习内容:
视频教程:
https://www.kancloud.cn/tpshop/thinkphp5/228987
完全开发手册:https://www.kancloud.cn/manual/thinkphp5/142358
教程中的代码:https://github.com/phpervip/tp5a
创建一个汽车表,每个用户有一部车。
CREATE TABLE tp5
.tp_car
(
id
INT(10) NOT NULL AUTO_INCREMENT,
user_id
INT NOT NULL,
brand
VARCHAR(45) NULL,
plate_number
VARCHAR(45) NULL,
PRIMARY KEY (id
, user_id
));
model模型中
public function car(){
return $this->hasOne('Car','user_id','id');
}
Controller控制器中
public function test19{
$user = mUser::get(1);
echo '车品牌:'.$user->car->brand."车牌号".$user->car->plate_number."
";
$user = new mUser;
$user->email = '123@qq.com';
$user->nickname = 'TPshop';
$user->password = '123456';
if($user->save()){
$car['brand']='奔驰';
$car['plate_number']='浙A12345';
}else{
}
$user = mUser::get(94,'car');
echo $user->email.'
';
echo $user->nickname.'
';
echo $user->car->brand.'
';
echo $user->car->plate_number.'
';
$user = mUser::get(94);
$user->email = 'Tpshop@qq.com';
if($user->save()){
$user->car->plate_number = '浙A12345';
$user->car->save();
return '用户['.$user->email.']更新成功';
}else{
return $user->getError();
}
$user = mUser::get(94);
if($user->delete()){
$user->car->delete();
return '用户['.$user->email.']删除成功';
}else{
return $user->getError();
}
}