```
新建
D:\\phpStudy\\WWW\\20201213\\application\\home\\model\\Profile.php
新建数据用create不能用save
字段为空,设置字段为text ,还是为空,修改页面字体GBK ->UTF-8
Use of undefined constant profile - assumed 'profile'
echo $user-profile;
改为
echo $user->profile;
如何使用
$user = UserModel::get(8);
//$user->profileone;
$user->profilemany;
return $user;
显示:
{"id":8,"username":"qian","password":"123","gender":"女","profilemany":\[{"id":1,"user\_id":8,"hobby":"吃"},{"id":2,"user\_id":8,"hobby":"吃2"}\]}
完成版
D:\\phpStudy\\WWW\\20201213\\application\\home\\model\\User.php
namespace app\\home\\model;
use think\\Model;
class User extends Model
{
public function profileone()
{
return $this->hasOne('profile','user\_id','id'); //附表 附表字段 主表字段
}
public function profilemany()
{
return $this->hasMany('profile','user\_id','id');
}
}
D:\\phpStudy\\WWW\\20201213\\application\\home\\model\\Profile.php
namespace app\\home\\model;
use think\\Model;
class Profile extends Model
{
}
D:\\phpStudy\\WWW\\20201213\\application\\home\\controller\\Grade.php
namespace app\\home\\controller;
use think\\Controller;
use app\\home\\model\\User as UserModel;
use app\\home\\model\\Profile as ProfileModel;
class Grade extends Controller
{
//http://127.0.0.1:1213/index.php/Grade
public function Index()
{
// $data = \[
// 'username' => 'zhang',
// 'password' => '123',
// 'gender' \=> '男'
// \];
$user = UserModel::get(8);
//$user->profileone;
$user->profilemany;
return $user;
}
//http://127.0.0.1:1213/index.php/Grade/saveok
public function saveok(){
$data = \[
'username' => 'qian',
'password' => '123',
'gender' \=> '女'
\];
$user = UserModel::create($data);
echo $user->id;
}
public function prosaveok(){
$data = \[
'user\_id' => '8',
'hobby' => '吃',
\];
$user =ProfileModel::create($data);
echo $user->id;
}
}
报错
Class 'app\\home\\model\\User\\Profile' not found
答表的命名空间不同
return $this->hasOne('app\\home\\model\\Profile\\Profile','user\_id','id');
一对多关联
Trying to get property of non-object
或者 Undefined property: think\\model\\relation\\HasMany::$hobby
dump($user->profilemany\->hobby); 或者 dump($user->profilemany()->hobby);
答一对多不能查询单个字段的值,这个查询是错误的. 加括号也是不行的
Call to a member function where() on array
return $user->profilemany->where('id','>','1')->select();
答:没有加小括号
return $user->profilemany()\->where('id','>','1')->select();
variable type error: array
没有用json输出
return json($user->profilemany()->where('id','>','1')->select());
method not exist:think\\db\\Query->profile
没有这个方法 改为
$user1 = UserModel::has('profilemany','>=','2')->select();
variable type error: array
答没有return为json格式.
Has方法,主要是对附表进行数量统计,来显示主表的结果.只针对一对多
SELECT \* FROM `tp\_user` `User` INNER JOIN `tp\_profile` `Profile` ON `User`.`id`=`Profile`.`user\_id` GROUP BY `Profile`.`user\_id` HAVING count(\*)>=2
一对多模型的函数, // 附表比较// 数量和
返回的是主表的结果 .
$user1 = UserModel::has('profilemany','>=','2')->select();
Haswhere方法
method not exist:think\\db\\Query->profile
$user2 = UserModel::hasWhere('profilemany',\['status'=>2\])->select();
SELECT `User`.\* FROM `tp\_user` `User` INNER JOIN `tp\_profile` `Profile` ON `User`.`id`=`Profile`.`user\_id` WHERE `Profile`.`status` = '2' GROUP BY `User`.`id`
根据附表的某字段相等的值,来显示主表的结果
Save方法
类的属性不存在:app\\home\\model\\User\\User->profile
原
return $user3->profile->save(\[
'hobby'=>'玩',
'status'=>'0'
\]);
改
return $user3->profilemany\->save(\[
'hobby'=>'玩',
'status'=>'0'
\]);
Call to a member function save() on array
改为
return $user3->profilemany()\->save(\[
'hobby'=>'玩',
'status'=>'0'
\]);
Method app\\home\\model\\Profile\\Profile::\_\_toString() must return a string value
Save不能直接return 必须要return $user3->id;
原
return $user3->profilemany()->save(\[
'hobby'=>'玩',
'status'=>'0'
\]);
改为
$user3 = UserModel::get(8);
$user3->profilemany()->save(\[
'hobby'=>'玩',
'status'=>'0'
\]);
return $user3->id;
数据库里没有中文数据
答:查看文件格式为GBK改为utf-8
Save先查询主表单条数据,再新增附表数据
$user3 = UserModel::get(8);
$user3->profilemany()->save(\[
'hobby'=> '玩',
'status'=> '0'
\]);
return $user3->id;
INSERT INTO `tp\_profile` (`hobby` , `status` , `user\_id`) VALUES ('玩' , 0 , 8)
Saveall
Illegal string offset 'user\_id'
$user4 = UserModel::get(8);
$user4->profilemany()->saveAll(
\['hobby'=>'乐','status'=>1\],
\['hobby'=>'乐1','status'=>0\]
);
//return $user4->id;
Illegal string offset 'user\_id'
\[2\]Illegal string offset 'user\_id'\[D:\\phpStudy\\WWW\\20201213\\thinkphp\\library\\think\\model\\relation\\HasMany.php:210\]
$user4->profilemany()->saveAll( \['hobby'=>'乐','status'=>1\],\['hobby'=>'乐','status'=>1\]);
改为
$user4->profilemany()->saveAll(array(\['hobby'=>'乐','status'=>1\],\['hobby'=>'乐','status'=>1\]));
或者
$user4->profilemany()->saveAll(\[\['hobby'=>'乐','status'=>1\],\['hobby'=>'乐','status'=>1\]\]);
Toregest
类的属性不存在:app\\home\\model\\User\\User->profile
原
$user5 = UserModel::get(6);
$user5->together('profile')->delete();
改为
$user5->together('profilemany')->delete();
Call to a member function together() on null
return json($user5); //null
已经删除这个这条数据了,没有这个ID=6的数据了
$user5 = UserModel::get(7);
$user5->together('profilemany')->delete();
只删除了主表user数据,并没有删除附表数据
改
$user5 = UserModel::get(7,’profile’);
$user5->together('profilemany')->delete();
method not exist:think\\db\\Query->profile
原
$user5 = UserModel::get(7,'profile');
$user5->together('profilemany')->delete();
改
$user5 = UserModel::get(7,'profilemany');
$user5->together('profilemany')->delete();
Call to a member function together() on null
改为
数据已经不存在了.
原
$user5 = UserModel::get(7,'profile');
$user5->together('profilemany')->delete();
改
$user5 = UserModel::get(8,'profilemany');
$user5->together('profilemany')->delete();
// $user5 = UserModel::get(8,'profilemany');
$user5 = UserModel::get(10,'profilemany');
json($user5);
//$user5->together('profilemany')->delete();
$user5->together(\['profilemany'\])->delete();
不能删除附表数据 关联删除只支持一对一模式,不能用于一对多模式
$user5 = UserModel::get(11,'profileone');
$user5->together(\['profileone'\])->delete();
method not exist:think\\db\\Query->all
答:没有这个方法,all只能用select传个数组
$user = UserModel::with('profilemany')->all(\[4,12,13\]);
foreach($user as $file)
{
print\_r($file->profilemany);
}
改为
$user = UserModel::with('profilemany')->select(\[4,12,13\]);
foreach($user as $file)
{
print\_r($file->profilemany);
}
或者改为
$user = UserModel::all(\[4,12,13\],'profilemany');
foreach($user as $file){
dump($file->profilemany);
}
Load方法 array用load\_relation 延时预载入
Call to a member function load() on array
$user = UserModel::all(\[4,12,13\]);
$user->load ('profile');
foreach($user as $file)
{
dump($file->profilemany);
}
改为
$user = UserModel::all(\[4,12,13\]);
$user\->load\_relation('profile');
foreach($user as $file)
{
dump($file->profilemany);
}
$user = UserModel::all(\[4,12,13\]);
$user \= load\_relation($user,'profile');
改为
$user = UserModel::all(\[4,12,13\]);
$user = load\_relation($user,'profilemany');
两个表查询
$user = UserModel::all(\[4,12,13\]);
$user = load\_relation($user,'profilemany,bookmany');
foreach($user as $file)
{
echo var\_dump($file->profilemany,$file->bookmany);
}
D:\\phpStudy\\WWW\\20201213\\application\\home\\model\\Book\\Book.php
namespace app\\home\\model\\Book;
use think\\Model;
class Book extends Model
{
}
D:\\phpStudy\\WWW\\20201213\\application\\home\\model\\User\\User.php
public function bookmany()
{
return $this->hasMany('app\\home\\model\\Book\\Book','user\_id','id');
}
Trying to get property of non-object
Foreach才行
//join
$user = UserModel::With('profileone')->select(\[4,12,13\]);
foreach($user as $file)
{
var\_dump($file->profileone);
}
return $this->hasOne('app\\home\\model\\Profile\\Profile','user\_id','id');
SELECT \* FROM `tp\_profile` WHERE `user\_id` IN (4,13,12)
//return $this->hasOne('app\\home\\model\\Profile\\Profile','user\_id','id')->setEagerlyType(0);
SELECT `user`.`id`,`user`.`username`,`user`.`password`,`user`.`gender`,profileone.id AS profileone\_\_id,profileone.user\_id AS profileone\_\_user\_id,profileone.hobby AS profileone\_\_hobby,profileone.status AS profileone\_\_status FROM `tp\_user` `user` INNER JOIN `tp\_profile` `profileone` ON `user`.`id`=`profileone`.`user\_id` WHERE `user`.`id` IN (4,12,13)
致命错误: Call to a member function goodstocatemany() on array
```
- extend加入模块
- array_column() expects parameter 1 to be array, object given
- tp5.1安装No input file specified.
- ROOT
- wechatDeveloper
- ThinkLibrary
- 语法错误: unexpected '?'
- Cannot use object of type stdClass as array
- Invalid argument supplied for foreach()
- 常量上传
- 命名空间extend_email_Aindex
- Validate
- 'cross-env' 不是内部或外部命令,也不是可运行的程序
- 内置规则
- composer self-update
- cURL error 60: SSL certificate problem
- GuzzleHttp_Client_cookie_true
- cookies原始字符串转换为数组
- 设置cookie
- apidoc
- 原生安装tp5
- array_unique
- explode
- tp3修改密码
- tp打印SQL
- tp5一对一关联
- allowField
- array_column
- array_key_exists
- array_search
- tp5如何引入自定义函数
- with
- with闭包
- 绑定类
- composer A
- array_filter用回调函数过滤数组中的元素
- array_keys
- array_slice截取前两个
- fetch_assoc
- in_array是否存在指定的值
- is_null
- join
- krsort对数组排序
- array-udiff
- shuffle
- substr
- unset删除多维数组key
- 多维转一维函数
- 数组变成字符串implode
- 数组分割
- 数组取差集
- 随机函数array_rand
- 一维变二维
- 字符串包裹变量
- 字符串汉字截取mb_substr
- 直接使用composer命令
- 安装vendor
- composer命令
- 复制安装
- 空目录安装
- tp5cms
- tp5调试
- tp3调试
- TP5一对一关联
- SQL语句
- 清空数据
- 去重
- 去重保留最小id
- 去重匹配除数字外数据
- 事务
- tp5pdf乱码
- tp5column_tp3getFieldtrue
- php代码在线测试,php在线执行
- parent::_initialize