Dai Chong's blog

tp5模型新增关联报错类的属性不存在,即使数据表中存在需要的字段,但是依然报错app\common\model\Slide->is_slide类似的问题,其实是因为在使用关联模型新增的时候主表新增操作未使用模型新增。

错误写法
1
2
3
4
5
//直接新增未使用模型
$result = Db::name('table')->allowField(true)->save($info);
//然后使用模型新增
$model = new \app\common\model\ShopCommentM();
$model->comment_image()->saveAll($imageData);

正确写法
1
2
3
4
//先使用模型新增主表,再新增关联表
$model = new \app\common\model\ShopCommentM();
$model->allowField(true)->save($info);
$model->comment_image()->saveAll($imageData);
模型文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//主表模型
<?php
namespace app\common\model;
use think\Model;

class ShopCommentM extends Model
{
protected $table = 'shop_comment';

public function comment_image()
{
return $this->hasMany('ShopCommentImageM','comment_id');
}
}
//关联表模型
<?php
namespace app\common\model;
use think\Model;

class ShopCommentImageM extends Model
{
protected $table = 'shop_comment_image';
}

 评论