0

Hi all i have a custom module check the screenshot below:-

enter image description here

now i have created a custom table in database with name the learn_checkdelivery here is the screenhsot

enter image description here

now i want these pincodes value in the database is to be reflected in my backend module field with the name Postcode. how to achieve this any suggestion or help. Thanks in Advance

Update

system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">

    <system>
        <tab id="learn" translate="label" sortOrder="100">
            <label>Learn</label>
        </tab>
        <section id="checkDelivery" translate="label" type="text" sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Check Delivery</label>
            <tab>learn</tab>
            <resource>Learn_CheckDelivery::config_checkdelivery</resource>
            <group id="config" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Check delivery postcode</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="postcodes" translate="label" type="textarea" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Postcodes</label>
                    <comment>Comma-separated</comment>
                    <depends><field id="enable">1</field></depends>
                </field>
                <field id="success_message" translate="label" type="text" sortOrder="21" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Available</label>
                    <depends><field id="enable">1</field></depends>
                </field>
                <field id="error_message" translate="label" type="text" sortOrder="22" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Unavailable</label>
                    <depends><field id="enable">1</field></depends>
                </field>
            </group>
        </section>

    </system>
</config>

Learn/CheckDelivery/Model/Pincode.php

<?php
namespace Learn\CheckDelivery\Model;

class Pincode extends \Magento\Framework\Model\AbstractModel
{
    public function _construct()
    {
        $this->_init('Learn\CheckDelivery\Model\ResourceModel\Pincode');
    }
}

Learn/CheckDelivery/Model/ResourceModel/Pincode.php

<?php
namespace Learn\CheckDelivery\Model\ResourceModel;

class Pincode extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    public function _construct()
    {
        $this->_init('learn_checkdelivery', 'id');
    }
}

Learn/CheckDelivery/Model/ResourceModel/Pincode/Collection.php

<?php
namespace Learn\CheckDelivery\Model\ResourceModel\Pincode;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection 
{
    protected function _construct()
    {
        $this->_init('Learn\CheckDelivery\Model\Pincode','Learn\CheckDelivery\Model\ResourceModel\Pincode');
    }
}

update

Textarea.php

<?php 
namespace Learn\CheckDelivery\Model\Form\Element;

class Textarea extends \Magento\Framework\Data\Form\Element\Textarea
{
    public function __construct
    (
        \Magento\Framework\Data\Form\Element\Factory $factoryElement,
        \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
        \Magento\Framework\Escaper $escaper,
        \Learn\CheckDelivery\Model\Pincode $pincode,
        $data = []
    ) 
    {
        $this->pincode = $pincode
        parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
        $this->setType('text');
        $this->setExtType('textfield');
    }

    public function getHtml()
    {
        $data = $this->pincode->getCollection()->getColumnValues('pincode');
        $data = implode(', ', $data);

        $this->addClass('input-text admin__control-text');

        $this->setValue($data);
        return parent::getHtml();
    }
}
  • do you want 'learn_checkdelivery' tables value in postcode system config ? – Hiren Feb 6 at 7:52
  • yes exactly this is what i want – Chikku Feb 6 at 8:04
  • update your question with code of system.xml – Hiren Feb 6 at 8:05
  • okay wait am on it – Chikku Feb 6 at 8:06
  • @Hiren please check my updated question – Chikku Feb 6 at 8:08
2

Update your system.xml as below

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">

    <system>
        <tab id="learn" translate="label" sortOrder="100">
            <label>Learn</label>
        </tab>
        <section id="checkDelivery" translate="label" type="text" sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Check Delivery</label>
            <tab>learn</tab>
            <resource>Learn_CheckDelivery::config_checkdelivery</resource>
            <group id="config" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Check delivery postcode</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="postcodes" translate="label" type="Vendor\Module\Model\Form\Element\Textarea" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Postcodes</label>
                    <comment>Comma-separated</comment>
                    <depends><field id="enable">1</field></depends>
                </field>
                <field id="success_message" translate="label" type="text" sortOrder="21" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Available</label>
                    <depends><field id="enable">1</field></depends>
                </field>
                <field id="error_message" translate="label" type="text" sortOrder="22" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Unavailable</label>
                    <depends><field id="enable">1</field></depends>
                </field>
            </group>
        </section>

    </system>
</config>

Add following file Vendor\Module\Model\Form\Element\Textarea.php

namespace Vendor\Module\Model\Form\Element;

class Textarea extends \Magento\Framework\Data\Form\Element\Textarea
{
    public function __construct
    (
        \Magento\Framework\Data\Form\Element\Factory $factoryElement,
        \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
        \Magento\Framework\Escaper $escaper,
        \Learn\CheckDelivery\Model\Pincode $pincode,
        $data = []
    ) 
    {
        $this->pincode = $pincode;
        parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
        $this->setType('text');
        $this->setExtType('textfield');
    }

    public function getHtml()
    {
        $data = $this->pincode->getCollection()->getColumnValues('pincode');
        $data = implode(', ', $data);

        $this->addClass('input-text admin__control-text');

        $this->setValue($data);
        return parent::getHtml();
    }
}

This will give you what exactly you want :D

| improve this answer | |
  • is my model resource model and collection file is correct ?? – Chikku Feb 6 at 8:29
  • have you got any errors ? you can follow Amit Bera's answer for model, resource model and collection – Hiren Feb 6 at 9:09
  • what changes have you made in system.xml file?/ – Chikku Feb 6 at 9:12
  • i have added 'Vendor\Module\Model\Form\Element\Textarea' as type, you can check code for further – Hiren Feb 6 at 9:16
  • @Pramod upvote and accept if it helps you ! – Hiren Feb 6 at 9:27
0

As you want to get fetch data from a database table , then you have to create

  1. Create a Model;
  2. Create a Resource Model
  3. Create Resource Model Collection

Magento2 use MVVM architecture,So you need to work on model section which includes Model, Resource Model,Resource Model Collection Class.

Model Class:

file Location: app/code/{Vendor}/{ModuleName}/Model/{YourModel}.php.

 <?php

/**
 * Create model  name of Student
 */
namespace {Vendor}\{ModuleName}\Model;


class {YourModel} extends \Magento\Framework\Model\AbstractModel
{
    /**
     * Map Resource Class At Model lass
     */
    public function _construct()
    {
        $this->_init(\{Vendor}\{ModuleName}\Model\ResourceModel\{YourModel}::class);
    }
}

Resource Model Class

file Location: app/code/{Vendor}/{ModuleName}/Model/ResourceModel/{YourResourceModelName}.php

And code:

<?php

/**
 * Create Resource Model name for model  Student \StackExchange\Example\Model\Student
 * As Model name Student then Resource model is Student
 */

namespace {Vendor}\{ModuleName}\Model\ResourceModel;


class {YourResourceModelName} extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

    /**
     * Define table for this Resource model
     * @return void
     */
    public function _construct()
    {
       $this->_init('learn_checkdelivery', 'id');
    }

}

Resource Model Collection

File Location: app/code/{Vendor}/{ModuleName}/Model/ResourceModel/{YourResourceModelName}/Collection.php

And Code

<?php

namespace {Vendor}\{ModuleName}\Model\ResourceModel\{YourResourceModelName};


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

   /**
    * Map Model CLass and Resource mode Class at COllection Class
    */
    public function _construct()
    {
        $this->_init(
                \{Vendor}\{ModuleName}\Model\{YourModel}::class,
                 \{Vendor}\{ModuleName}\Model\ResourceModel\{YourResourceModelName}::class
                );
    }

}

After that using the Resource Collection model and Model Class you fetch data from that table.

https://www.mageplaza.com/magento-2-module-development/how-to-create-crud-model-magento-2.html#step-2-create-model

| improve this answer | |
  • i have already tried this way and its not working – Chikku Feb 6 at 8:05
  • Please share your of these classes,I will check – Amit Bera Feb 6 at 8:10
  • check my updated question thanks – Chikku Feb 6 at 8:20

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.