0

How can I use join table to display second table data in admin grid? Admin grid is created using layout & block in proper manner.

I used this link (how to create a grid without using ui component in magento 2) as reference.

Using below code I display data

<block class="Magento\Backend\Block\Widget\Grid\Column" name="recipient_email">
                    <arguments>
                        <argument name="header" xsi:type="string" translate="true">Recipient Email</argument>
                        <argument name="index" xsi:type="string">recipient_email</argument>
                        <argument name="renderer" xsi:type="string">Comapny\Module\Block\Adminhtml\Widget\Grid\Column\Renderer\Recipientemail</argument>

                </block>

But issue while searching & sorting

I am using this for overriding the default module of enterprise (gift card account)

Code of

Company/Module/Block/Adminhtml/Widget/Grid/Column/Renderer/Recipientemail.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Company\Module\Block\Adminhtml\Widget\Grid\Column\Renderer;

class Recipientemail extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
    protected $_storeManager;


    public function __construct(
        \Magento\Backend\Block\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,      
        \Magento\Sales\Model\Order\Item $orderItem,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_storeManager = $storeManager;      
        $this->orderitem =  $orderItem;  
    }


    public function render(\Magento\Framework\DataObject $row)
    {

        $code = $row->getCode();
        $result = array();
        $salesorderitem =  $this->orderitem->getCollection()
                          ->addFieldToFilter('product_options', array('like' => '%'.$code.'%'));

        if(!empty($salesorderitem)) 
        {
            foreach($salesorderitem as $orderdata){
                $productOptions = $orderdata->getProductOptions();   
                $recipientemail = $productOptions['giftcard_recipient_email'];
                if(!empty($recipientemail)){
                return $recipientemail;
                }else{
                    return '-';
                }
            }
        }else{
               return '-';
        }


    }
    public function getContents($str, $startDelimiter, $endDelimiter) {
        $contents = array();
        $startDelimiterLength = strlen($startDelimiter);
        $endDelimiterLength = strlen($endDelimiter);
        $startFrom = $contentStart = $contentEnd = 0;
        while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
          $contentStart += $startDelimiterLength;
          $contentEnd = strpos($str, $endDelimiter, $contentStart);
          if (false === $contentEnd) {
            break;
          }
          $contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
          $startFrom = $contentEnd + $endDelimiterLength;
        }

        return $contents;
      }

}

Code for Adminhtml\Grid\Filter\Recipientemail.php

<?php

namespace Comapny\Module\Block\Adminhtml\Grid\Filter;

class Recipientemail extends \Magento\Backend\Block\Widget\Grid\Column\Filter\Text {

public function getCondition()
{   
    if ($this->getValue() === null) {
        return null;
    }

    return null;
}

}

  • This will help you magento.stackexchange.com/questions/182146/… – Chirag Patel Jul 15 at 5:14
  • @ChiragPatel The issue with me is that I need to add this functionality in enterprise giftcard module which is not using UI component to display grid & I am not aware with how to filter the grid with second database that also from sales_order_item 'product_options' – Arjun Jul 15 at 5:31
  • Are you able to display correct data in the grid after join tables? – Chirag Patel Jul 15 at 5:40
  • I am able to display correct data using xml & /Renderer/Recipientemail.php code but the issue occurs during filter in Filter/Recipientemail.php. I am getting search text box value in Filter/Recipientemail.php but no idea about how to reload this grid to display data based on filter – Arjun Jul 15 at 5:45
  • share your join table code. – Chirag Patel Jul 15 at 5:48
0

Try this code

<block class="Magento\Backend\Block\Widget\Grid\Column" name="recipient_email">
                <arguments>
                    <argument name="header" xsi:type="string" translate="true">Recipient Email</argument>
                    <argument name="index" xsi:type="string">recipient_email</argument>
                    <argument name="filter" xsi:type="string">Comapny\Module\Block\Adminhtml\Grid\Filter\Recipientemail</argument>
                    <argument name="renderer" xsi:type="string">Comapny\Module\Block\Adminhtml\Widget\Grid\Column\Renderer\Recipientemail</argument>

</block>

Comapny\Module\Block\Adminhtml\Grid\Filter\Recipientemail

<?php

    namespace Comapny\Module\Block\Adminhtml\Grid\Filter;

    class Recipientemail extends \Magento\Backend\Block\Widget\Grid\Column\Filter\Text
    {

        public function getCondition()
        {
            if ($this->getValue() === null) {
                return null;
            }

            return ['eq' => $this->getValue()];
        }
    }
  • <argument name="index" xsi:type="string">recipient_email</argument>. Here recipient_email is not in my main table how can filter it. It gives error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'recipient_email' in 'where clause', query was: SELECT COUNT(*) FROM magento_giftcardaccount AS main_table WHERE (recipient_email = 'abc') – Arjun Jul 13 at 13:20
  • Can you please provide me this file code Comapny\Module\Block\Adminhtml\Widget\Grid\Column\Renderer\Recipientemail – Vijay Khirade Jul 13 at 13:23
  • Hi @Vijay I have added code in my question – Arjun Jul 15 at 4:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.