0

Block file

<?php
    namespace vendor/module/Block;



    use Magento\Framework\View\Element\Template;

    class Postform extends Template
    {

            protected $_connection;
            const CONFIG_CAPTCHA_PUBLIC_KEY = 'quickrfq/google_options/googlepublickey';
            const CONFIG_CAPTCHA_THEME = 'quickrfq/google_options/theme';
            const CONFIG_CAPTCHA_ENABLE = 'quickrfq/google_options/captchastatus';

            const CONFIG_FILE_EXT_UPLOAD = 'quickrfq/upload/allow';

        protected $scopeConfig;    
        public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\ResourceConnection $resource
        )
        {
            $this->scopeConfig = $context->getScopeConfig();
            $this->_connection = $resource->getConnection();
            parent::__construct($context);
        }

        public function getTableData()
        {    

        $myTable = $this->_connection->getTableName('fme_quickrfq');
        $sql     = $this->_connection->select()->from(
             ["tn" => $myTable]
        ); 
        $result  = $this->_connection->fetchAll($sql);
        return $result;
        }  


        public function getFormAction()
        {
            return $this->getUrl('quickrfq/index/post', ['_secure' => true]);
        }

        public function getCaptchaTheme()
        {

            $theme = $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_THEME);
            return $theme;
        }

        public function isCaptchaEnable()
        {

            $enable = $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_ENABLE);
            return $enable;
        }

        public function getAllowedFileExtensions()
        {

            $ext = $this->scopeConfig->getValue(self::CONFIG_FILE_EXT_UPLOAD);
            return $ext;
        }
        public function getPublicKey()
        {

            return $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_PUBLIC_KEY);
        }
    }

phtml file

 <?php
    $blockObj= $block->getLayout()->createBlock('vendor\module\Block\Postform');
    print_r($blockObj->getTableData());

    ?>

installschema.php

<?php

namespace vednor\module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{



    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {


        $installer = $setup;
        $installer->startSetup();


        /**
                        * Create table 'quickrfq'
                */

        $table = $installer->getConnection()->newTable($installer->getTable('fme_quickrfq'))
                                ->addColumn(
                                    'quickrfq_id',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                                    null,
                                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                                    'Quickrfq ID'
                                )
                                ->addColumn(
                                    'company',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    ['nullable' => true, 'default' => null],
                                    'Company'
                                )
                                ->addColumn(
                                    'contact_name',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    [],
                                    'Contact Name'
                                )
                                ->addColumn(
                                    'phone',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    [],
                                    'Phone'
                                )
                                ->addColumn(
                                    'email',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    [],
                                    'Email'
                                )
                                ->addColumn(
                                    'project_title',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    [],
                                    'Project Title'
                                )
                                ->addColumn(
                                    'date',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
                                    null,
                                    [],
                                    'Date'
                                )
                                ->addColumn(
                                    'budget',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    [],
                                    'Budget'
                                )
                                ->addColumn(
                                    'overview',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    [],
                                    'Overview'
                                )
                                ->addColumn(
                                    'prd',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    [],
                                    'Prd'
                                )
                                ->addColumn(
                                    'status',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    ['nullable' => false, 'default' => 'New'],
                                    'Status'
                                )
                                ->addColumn(
                                    'sku',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    255,
                                    ['nullable' => false, 'default' => null],
                                    'sku'
                                )
                                ->addColumn(
                                    'create_date',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                                    null,
                                    ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
                                    'Creation Date'
                                )
                                ->addColumn(
                                    'update_date',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                                    null,
                                    ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
                                    'Update Date'
                                );


        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }
}

result in customer dashboard under quote enter image description here

explaination:-

i have a custom module where am getting data from table and displaying it on customer dashboard under my quote tab. now with this block code it displays all the data available in table. what i want is to display the data of a customer by matching it with the email id he has signed in. means i want to show the data of customer whose email id match with email he has logged in. any advice on this will be a great help thanks

1
  • please check and update me.
    – Msquare
    Mar 28, 2020 at 13:29

2 Answers 2

0

Use Model for custom table data manipulation

app/code/VendoreName/ModuleName/Model

CustomerData.php

<?php

namespace VendoreName\ModuleName\Model;

use Magento\Framework\Model\AbstractModel;
use VendoreName\ModuleName\Model\ResourceModel\CustomerData as CustomerDataResourceModel;

class CustomerData extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(CustomerDataResourceModel::class);
    }
}

app/code/VendoreName/ModuleName/Model/ResourceModel

CustomerData.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class CustomerData extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('fme_quickrfq', 'quickrfq_id');
    }
}

app/code/VendoreName/ModuleName/Model/ResourceModel/CustomerData

Collection.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel\CustomerData;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use VendoreName\ModuleName\Model\CustomerData as CustomerDataModel;
use VendoreName\ModuleName\Model\ResourceModel\CustomerData as CustomerDataResourceModel;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(
            CustomerDataModel::class,
            CustomerDataResourceModel::class
        );
    }
}

And get your customer with matching email

protected $customerSession;
protected $CollectionData;


public function __construct(
    .............................................
    \VendoreName\ModuleName\Model\Session $customerSession,
    \VendoreName\ModuleName\Model\ResourceModel\CustomerData\CollectionFactory $CollectionData,
    .............................................
) {
    .............................................
    $this->customerSession = $customerSession;
    $this->CollectionData = $CollectionData;
    .............................................
}

public function getCustomerData()
{   
    // get current customer email 
    $customerEmail = $this->customerSession->getCustomer()->getEmail();

    //get custom table data
    $customerData = $this->CollectionData->create();
    $customerData->addFieldToFilter('email',$customerEmail);
    //print_r($customerData->getData());        

    return $customerData;
}

I Hope This Helps You.

0

Inject below class

protected $customerSession;

public function __construct(
    ...................,
    \Magento\Customer\Model\Session $customerSession,
    .................
) {
    parent::__construct(.......);
    $this->customerSession = $customerSession;
}

Then modify your function getTableData()

public function getTableData()
  {    
  $customerId = $this->customerSession->getCustomer()->getId();
    if(isset($customerId) && $customerId != ''){
        $customerEmail = $this->customerSession->getCustomer()->getEmail();
        $myTable = $this->_connection->getTableName('fme_quickrfq');
        $sql     = $this->_connection->select()->from(
             ["tn" => $myTable]
        )->where('tn.email? ',$customerEmail );
        $result  = $this->_connection->fetchAll($sql);
        return $result;
    }
    return;
}  

make sure to change your column here, tn.entity_id customerId column in your table "fme_quickrfq".

Hope this may help you!!

12
  • if(isset($customerId) && $customerId != '') can you please explain me this line
    – Pramod
    Mar 4, 2020 at 7:28
  • That line checks whether the customer is logged in or not, if logged in then it gets that customer data, else empty data will return, make a try with my code, it will work Mar 4, 2020 at 7:32
  • no its not working coz i dnt have customer id field in my custom table
    – Pramod
    Mar 4, 2020 at 7:42
  • then is there any column in your custom table related to customer? Mar 4, 2020 at 7:44
  • i want to match logged in customer email with email in table
    – Pramod
    Mar 4, 2020 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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