Laravel-检索多对多多态关系的逆关系(分页) [英] Laravel - Retrieve the inverse of a many-to-many polymorphic relation (with pagination)

查看:424
本文介绍了Laravel-检索多对多多态关系的逆关系(分页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一些挖掘,我仍然找不到任何可靠的方法来检索允许混合模型结果的多对多多态关系的逆.

after some digging I still could not find any solid way to retrieve the inverse of a many-to-many polymorphic relation that allows mixed models results.

请考虑以下内容:

我有几个可以标记"的模型.虽然检索例如$item->tags$article->tags和带有$tag->articles$tag->items的逆数是微不足道的,但我没有简单的方法来执行类似$tag->taggables的操作来返回同一集合中的商品和商品.事情变得更加艰难,因为我需要对查询使用分页/简单分页.

I have several models that can be "tagged". While it is trivial to retrieve for example $item->tags, $article->tags and the inverse with $tag->articles and $tag->items I have no easy way to do something like $tag->taggables to return both articles and items in the same collection. Things get even bumpier as I need to use pagination/simple pagination to the query.

我已经尝试了一些解决方法,但是我可以综合考虑的最好方法仍然显得局促和局限.基本上:

I have tried a few workarounds but the best I could put together still looks crappy and limited. Basically:

  • 我每个可标记"查询数据库一次;
  • 将所有内容放入一个大集合中;
  • 将集合传递给phpleague/fractal转换器(我的API使用了它),该转换器根据所解析的模型返回不同的json值.

这种方法的局限性在于,建立分页是一场噩梦,分形的"include"选项不能开箱即用.

The limits of this approach is that building a pagination is a nightmare and fractal "include" options can't be used out of the box.

有人可以帮助我吗?我目前正在使用Laravel 5.1.

Can anyone help me? I'm currently using Laravel 5.1.

我当前的代码中没有太多魔术.伪造并简化它以使其简短:

There is not much magic in my current code. Faking and simplifying it to make it short:

从api控制器中:

$tag = Tag::findOrDie($tid);
$articles = $tag->cms_articles()->get();
$categories = $tag->cms_categories()->get();
$items = $tag->items()->simplePaginate($itemsperpage);

$taggables = Collection::make($articles)->merge($categories);
// Push items one by one as pagination would dirt the collection struct.
foreach ($items as $item) {
    $taggables->push($item);
}

return $this->respondWithCollection($taggables, new TaggableTransformer);

注意:使用simplePaginate()只是因为我希望所有文章和类别都显示在首页加载中,而需要分页的项目数量太多.

来自Transformer类:

public function transform($taggable)
{
    switch (get_class($taggable)) {
        case 'App\Item':
            $transformer = new ItemTransformer;
            break;
        case 'App\CmsArticle':
            $transformer = new CmsArticleTransformer;
            break;
        case 'App\CmsCategory':
            $transformer = new CmsCategoryTransformer;
            break;
    }
    return $transformer->transform($taggable);
}

请注意,其他转换器只是返回有关它们与之相关的模型的数据数组.如果使用分形,您会很容易发现嵌套的包含"模型将不会被应用.

Please consider that the other transformers are simply returning arrays of data about the models they correlate with. If you use Fractal you would easily spot that nested "included" models would not be applied.

Tag模式没什么用

class Tag extends Model
{   
    protected $morphClass   = 'Tag';
    protected $fillable = array('name', 'language_id');

    public function cms_articles() {
        return $this->morphedByMany('App\CmsArticle', 'taggable');
    }

    public function cms_categories() {
        return $this->morphedByMany('App\CmsCategory', 'taggable');
    }

    public function items() {
        return $this->morphedByMany('App\Item', 'taggable');
    }

    // Would love something like this to return inverse relation!! :'(
    public function taggables() {
        return $this->morphTo();
    }
}

我还考虑选择对API进行3次单独调用以分三步检索文章,类别和项目.尽管在这种特殊情况下这也许是有道理的,但我仍然需要在项目的另一部分中处理这种特别的逆关系:通知.在这种特殊情况下,通知必须与许多不同的动作/模型相关,而我必须成批(分页)检索所有动作/模型,并按模型创建日期进行排序...

I am also considering the option to do 3 separate calls to the API to retrieve articles, categories and items in three steps. While in this particular scenario this might make sense after all, I would still need to deal with this particular inverse relation headache with another part of my project: notifications. In this particular case, notifications would have to relate to many different actions/models and I would have to retrieve them all in batches (paginated) and sorted by model creation date...

希望这一切都有道理.我不知道对整个逆多态"问题采用完全不同的方法是否会有所帮助.

Hope this all makes sense. I wonder if a completely different approach to the whole inverse "polymorphic" matter would help.

亲切的问候, 费德里科

Kind regards, Federico

推荐答案

是的.我不是很久以前就走在你的路上.在解决多态关系的逆关系时,我同样面临着噩梦.

Ah yes. I was down your path not all that long ago. I had the same nightmare of dealing with resolving the inverse of the relationship of polymorphic relationships.

不幸的是,在Laravel生态系统中,多态关系并没有得到足够的重视.从远处看,它们看起来像独角兽和彩虹,但不久之后您就在与这种事情作斗争.

Unfortunately polymorphic relationships haven't been given much attention in the Laravel ecosystem. From afar they look like unicorns and rainbows but soon you're fighting things like this.

您可以发布$thing->taggable的示例以获得更好的图片吗?认为它可以通过动态特质+辅助魔法解决.

Can you post an example of a $thing->taggable for a better picture? Think it may be solvable with a dynamic trait + accessor magic.

这篇关于Laravel-检索多对多多态关系的逆关系(分页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆