0

want to add join query in getPost function in a single view

 public function getPost()
    {
        if ($this->_post === null) {
            /** @var Post $post */
            $post = $this->_postFactory->create();

           $post->getSelect()->join( array('customer_entity'=> 'customer_entity'), 'customer_entity.entity_id = main_table.entity_id');

            $post->load($this->_getPostId());


            if (!$post->getId()) {
                throw new LocalizedException(__('Trails not found'));
            }

            $this->_post = $post;
        }
        return $this->_post;
    }

getting error

PHP Fatal error:  Uncaught Error: Call to a member function join() on null 

Magento CLI version 2.2.3

block page

namespace xxxx\Trails\Block;

use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Magento\Framework\Registry;
use \xxxx\Trails\Model\Post;
use \xxxx\Trails\Model\PostFactory;
use \xxxx\Trails\Controller\Post\View as ViewAction;

class View extends Template
{
    /**
     * Core registry
     * @var Registry
     */
    protected $_coreRegistry;

    /**
     * Post
     * @var null|Post
     */
    protected $_post = null;

    /**
     * PostFactory
     * @var null|PostFactory
     */
    protected $_postFactory = null;

    /**
     * Constructor
     * @param Context $context
     * @param Registry $coreRegistry
     * @param PostFactory $postCollectionFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $coreRegistry,
        PostFactory $postFactory,
        array $data = []
    ) {
        $this->_postFactory = $postFactory;
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $data);
    }

    /**
     * Lazy loads the requested post
     * @return Post
     * @throws LocalizedException
     */
    public function getPost()
    {
        if ($this->_post === null) {
            /** @var Post $post */
            $post = $this->_postFactory->create();
            $post->getSelect()->join( array('customer_entity'=> 'customer_entity'), 'customer_entity.entity_id = main_table.entity_id');
            $post->load($this->_getPostId());
            if (!$post->getId()) {
                throw new LocalizedException(__('Trails not found'));
            }
            $this->_post = $post;
        }
        return $this->_post;
    }

    /**
     * Retrieves the post id from the registry
     * @return int
     */
    protected function _getPostId()
    {
        return (int) $this->_coreRegistry->registry(
            ViewAction::REGISTRY_KEY_POST_ID
        );
    }


}
  • What is the class you inject into _postFactory? – PY Yick May 31 '18 at 7:30
  • 1
    @PYYick added block page – Nitesh Kumar Singh May 31 '18 at 7:40
  • Use this $post = $this->_postFactory->create()->getCollection(); – Dhaduk Mitesh May 31 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.