php中文网最新课程
每日17点准时技术干货分享
Sight则是提供了很好的Pluck函数,查出ID后,请求到相关数据交给Sight,Sight会为你拼接好数据。它的做法是通过关联ID为KEY把数据整理好。从而大大提升了程序效率。
namespace App\Presenter
use Bardoqi\Sight\Presenter;
use Bardoqi\Sight\Traits\PresenterTrait;
use Bardoqi\Sight\Enums\MappingTypeEnum
use Bardoqi\Sight\Enums\PaginateTypeEnum
use App\Repositories\ArticleRepository;
use App\Repositories\UserRepository;
class ArticlePresenter extents Presenter
{
use PresenterTrait;
public function getArticleList($where)
{
$articleArray = ArticleRepository::getList($where);
$user_ids = $this->selectFields('id','title','created_at','created_by')
->fromLocal($articleArray,'articles')
->pluck('created_by');
$users = UserRepository::getUsersWithIds($user_ids);
$this->innerJoinForeign($users,'userss')
->onRelationByObject(Relation::of()
->localAlias('articles')
->localField('created_by')
->foreignAlias('users')
->foreighField('id'))
->addFieldMappingByObject(FieldMapping::of()
->key('created_at')
->src('created_at')
->type(MappingTypeEnum::METHOD_NAME))
->addFieldMappingByObject(FieldMapping::of()
->key('created_by')
->src('user_name')
->type(MappingTypeEnum::JOIN_FIELD));
return $this->toPaginateArray(PaginateTypeEnum::PAGINATE_API);
}
}
Github 地址:https://github.com/BardoQi/Sight
▼请点击下方:“阅读原文”,在线查看!
文章评论