Thinkphp6with的⽤法详解预载⼊查询
表:
word
word_remember
对应的Model
WordRememberModel
WordModel
WordModel代码
WordRememberModel代码
<?php
declare (strict_types = 1);
namespace app\common\model;
use think\Model;
class WordRememberModel extends Model
{
protected $name = 'word_remember';
protected $pk = 'id';
public function word(){
return $this->hasOne(WordModel::class, 'id', 'member_id');
}
}
控制器
public function myVocabulary(WordRememberModel $obj){
$list = WordRememberModel::with(['word'])->select()->toArray();
print_r($list);
exit;
}
正常输出
Array ( [0] => Array ( [word_id] => 1 [member_id] => 1 [word] => Array ( [id] => 1 [title] => a ) ) )
数据库表结构
lqbcms_word_remember
CREATE TABLE `lqbcms_word_remember` (
`word_id` int(11) NOT NULL DEFAULT '0',
`member_id` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='例⼦';
lqbcms_word
CREATE TABLE `lqbcms_word` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(20) CHARACTER SET utf8 NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=103994 DEFAULT CHARSET=utf8mb4 COMMENT='英语单词';以上是Thinkphp6 with的⽤法详解
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论