cancel
Showing results for 
Search instead for 
Did you mean: 

How to merge two collections in Model

How to merge two collections in Model

I want to merge my two collections,

 

public function getRelatedPosts()
    {
        if (!$this->hasData('related_posts')) {
            /** collection 1 */
            $collection = $this->_relatedPostsCollection
                ->addFieldToFilter('post_id', ['neq' => $this->getId()])
                ->addStoreFilter($this->getStoreId());
            $collection->getSelect()->joinLeft(
                ['rl' => $this->getResource()->getTable('magefan_blog_post_relatedpost')],
                'main_table.post_id = rl.related_id',
                ['position']
            )->where(
                'rl.post_id = ?',
                $this->getId()
            );
         
             /** collection 2 */
            $collection = $this->_relatedPostsCollection
            ->addFieldToFilter('post_id', ['neq' => $this->getId()])
            ->addStoreFilter($this->getStoreId())
              ->addFieldToFilter('category_id', ['in' => $this->getCategories()]);
          $collection->getSelect()->columns(['position' => 'post_id']);
          $collection->getSelect()->joinLeft(
                         ['cl' => $this->getResource()->getTable('magefan_blog_post_relatedpost')],
                         'main_table.post_id != cl.related_id',
                         ['auto_related']);


            $this->setData('related_posts', $collection);

        }


        return $this->getData('related_posts');
    }

How to do that?