cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2 Backend - Pass ID FK from GridView Master to GridView Details

Magento2 Backend - Pass ID FK from GridView Master to GridView Details

I have to pass a FK parameter from GridView Master to GridView Details.

 

GridView Master (GVM):

GVM.png

 

 

GridView Details (GVD): 

GVD.png

 

PROBLEM: After click on 'Questions' on GVM for ID = 22, in GVD is showed all details of table (I want show only details linked to parent for ID = 22).

 

For GVD I have this controller called after click on action 'Questions' (on method execute I registry the FK:

 

/app/code/Alkemy/ChatBot/Controller/Adminhtml/QuestionSetFlow/Index.php

 

<?php

namespace Alkemy\ChatBot\Controller\Adminhtml\QuestionSetFlow;

class Index extends \Alkemy\ChatBot\Controller\Adminhtml\QuestionSetFlow
{

    protected $_backendSession;

    protected $_resultPageFactory;


    protected $_resultPage;

    protected $_questionSetId;

    public function __construct(
        \Magento\Backend\Model\Session $backendSession,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Alkemy\ChatBot\Model\QuestionSetFlowFactory $questionsetflowFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Backend\App\Action\Context $context
    )
    {
        $this->_backendSession    = $backendSession;
        $this->_resultPageFactory = $resultPageFactory;
        $this->_resultJsonFactory = $resultJsonFactory;
        parent::__construct($questionsetflowFactory, $registry, $resultRedirectFactory, $context);
    }

    public function execute()
    {
        //recupero ID of question set
        $id = $this->getRequest()->getParam('questionset_id');
        //Registry Question Set Id selected
        $questionsetId  = (int) $this->getRequest()->getParam('questionset_id');
        $this->_coreRegistry->register('questionset_id', $questionsetId);
        //$this->saveQuestionSetIdSelected();
        $questionsetFlow = $this->_questionsetFlowFactory->create();
        $questionset = $questionsetFlow->selectQuestionSetById($id);
        if (!isset($questionset) || sizeof($questionset) == 0 ) {
                $this->messageManager->addError(__('This Question set no longer exists.'));
                $resultRedirect = $this->_resultRedirectFactory->create();
                $resultRedirect->setPath('alkemy_chatbot/questionset/index', ['questionset_id' => $id]);
                $this->_questionSetId = $id;
                return $resultRedirect;

        }
//        $qustionSetFlowModel = $this->_objectManager->create('Alkemy\ChatBot\Model\QuestionSetFlow');
//        $qustionSetFlowModel->getCollection()->addFilter('questionset_id', $id);
        $this->_setPageData($questionset);
        return $this->getResultPage();
    }

    public function getResultPage()
    {
        if (is_null($this->_resultPage)) {
            $this->_resultPage = $this->_resultPageFactory->create();
        }
        return $this->_resultPage;
    }

    protected function _setPageData($questionset)
    {
        $resultPage = $this->getResultPage();
        //mostro il nome della Question Set come titolo
        $name = $questionset;
        if (isset($questionset) && is_array($questionset)){
            $temp = array_values($questionset)[0];
            $name = $temp['name'];
        }
        $resultPage->getConfig()->getTitle()->prepend((__($name)));
        return $this;
    }
}

 

Model for GVD is (in construct I set the FK):

/app/code/Alkemy/ChatBot/Model/QuestionSetFlow.php
<?php

namespace Alkemy\ChatBot\Model;

class QuestionSetFlow extends \Magento\Framework\Model\AbstractModel implements \Alkemy\ChatBot\Api\Data\QuestionSetFlowInterface,
    \Magento\Framework\DataObject\IdentityInterface
{
    const CACHE_TAG = 'alkemy_chatbot_questionsetflow';
    protected $_cacheTag = 'alkemy_chatbot_questionsetflow';
    protected $_eventPrefix = 'alkemy_chatbot_questionsetflow';

    //Id di QuestionSet
    protected $_questionSetId;

    public function __construct(\Magento\Framework\Model\Context $context,
                                \Magento\Framework\Registry $registry,
                                \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
                                \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
                                array $data = [])
    {

        array_push($data, $registry->registry('questionset_id'));
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
        //setto id del padre (FK)
        $this->setFK($registry->registry('questionset_id'));
    }

    /**
     *
     */
    protected function _construct()
    {
        $this->_init('Alkemy\ChatBot\Model\ResourceModel\QuestionSetFlow');
    }

    public function selectQuestionSetById($id)
    {
        return $this->getResource()->selectQuestionSetById($id);
    }

    /**
     * @param $id
     */
    public function setFK($id)
    {
        $this->_questionSetId = $id;
        //call Resource MOdel
        $this->getResource()->setQuestionSetId($id);
    }

}

 

Resource Model for GVD is:

/app/code/Alkemy/ChatBot/Model/ResourceModel/QuestionSetFlow.php
namespace Alkemy\ChatBot\Model\ResourceModel;

class QuestionSetFlow extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{


    protected $_date;

    //FK
    protected $_questionSetId;

    public function __construct(
        \Magento\Framework\Stdlib\DateTime\DateTime $date,
        \Magento\Framework\Model\ResourceModel\Db\Context $context
    )
    {
        $this->_date = $date;
        parent::__construct($context);
    }

    protected function _construct()
    {
        $this->_init('alkemy_chatbot_questionsetflow', 'questionsetflow_id');
    }


    public function selectQuestionSetById($id)
    {
        $dbh = $this->getConnection();
        $tableName = $this->getTable(\Alkemy\ChatBot\Setup\InstallSchema::alkemy_chatbot_questionset);
        $binds = ['id' => (int)$id];
        $select = $dbh->select()->from($tableName)
            ->where('questionset_id = :id')
        ;
        return $dbh->fetchAll($select, $binds);
    }


    public function setQuestionSetId($questionSetId)
    {
        $this->_questionSetId = $questionSetId;
    }

}

 

Then I have Collection for GVD:

/app/code/Alkemy/ChatBot/Model/ResourceModel/QuestionSetFlow/Collection.php
namespace Alkemy\ChatBot\Model\ResourceModel\QuestionSetFlow;

use Alkemy\ChatBot\Model\QuestionSetFlow;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{


    protected $_idFieldName = 'questionsetflow_id';

    protected $_eventPrefix = 'alkemy_chatbot_questionsetflow_collection';

    protected $_eventObject = 'questionsetflow_collection';


    protected function _construct()
    {
        $this->_init('Alkemy\ChatBot\Model\QuestionSetFlow', 'Alkemy\ChatBot\Model\ResourceModel\QuestionSetFlow');
    }


    protected function _renderFiltersBefore()
    {
        //find model
        $questionSetFlowModel = $this->_entityFactory->create(QuestionSetFlow::class);
        //fin fk in model
        $fk = $questionSetFlowModel->getFK();
        //set parent (FK) for filter
        $this->addFilter('questionset_id', $fk );
        //view only questions for filter
        $this->addFieldToFilter('question', [['notnull' => true]]);
        parent::_renderFiltersBefore();
    }
}

In DEBUG, I verified that method _renderFiltersBefore is called two time:

 

FIRST TIME:

$fk = 22
route = alkemy_chatbot
path = /admin/alkemy_chatbot/questionsetflow/index/questionset_id/22/key/7d1db25520ab7012a61f01e9b1b8c8e4b8ec97264003eef8dc41512a80eaa2dd/

SECOND TIME:

$fk = null
route = mui
path: /admin/mui/index/render/handle/bulk_bulk_details_modal/buttons/1/key/ee8f7151c618aebb613d21b0870ae0d6e8a910d6947ac9e9cdc57ce4c1925e65/

Why? Help me please Smiley Sad