- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2021
11:39 PM
03-21-2021
11:39 PM
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?
Labels: