0

Below is code For DataProvider in Edit form. Here it displays main tables data while i choose edit them. Can any one tell how to display data of another table

 <?php
 namespace My\Module\Model\Save;
 use My\Module\Model\ResourceModel\Save\CollectionFactory;
 class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
/**
 * @param string $name
 * @param string $primaryFieldName
 * @param string $requestFieldName
 * @param CollectionFactory $contactCollectionFactory
 * @param array $meta
 * @param array $data
 */

public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    CollectionFactory $contactCollectionFactory,
    array $meta = [],
    array $data = []
) {
    $this->collection = $contactCollectionFactory->create();

    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }

    $items = $this->collection->getItems();
    $this->loadedData = array();

    /** @var Customer $customer */
    foreach ($items as $contact) {
        // notre fieldset s'apelle "contact" d'ou ce tableau pour que magento puisse retrouver ses datas :
        $this->loadedData[$contact->getId()]['contact'] = $contact->getData();
    }


    return $this->loadedData;

}
}

this is my collection

 <?php

 namespace My\Module\Model\ResourceModel\Save;

 use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

 class Collection extends AbstractCollection
{
public function _construct()
{
    $this->_init('My\Module\Model\Save', 'My\Module\Model\ResourceModel\Save');

}
}

where my second table name is my_module_contact_add and fields i want to join is address, zipcode and country

0

The "ugly" method. You can try to just load collection of second table and assign columns you want.

Better way is to use for example leftJoin on collection init

     public function _initSelect()
     {
        parent::_initSelect();

        $this->getSelect()->joinLeft(
        ['secondTable' => $this->getTable('SECOND_TABLE_NAME')],
        'main_table.COULMN_NAME_TO_JOIN_TABLE1 = secondTable.COULMN_NAME_TO_JOIN_TABLE2',
        ['COLUMN_U_WANT_TO_ADD'=>'COLUMN_NAME_OF_NEW_COLUMN']
        );
        $this->addFilterToMap('COLUMN_U_WANT_TO_ADD', 'secondTable.COLUMN_NAME_OF_NEW_COLUMN');
     }
  • Where to Use this code? – Hardik Radadiya Aug 19 '19 at 6:10
  • On collection init. I created my own collection, (CRUD), and in Collection.php there is function _initSelect() which loads collection from db. There after collection loads i added this code to join column from other table I'm not sure this is the proper way, but works for me – lama377 Aug 19 '19 at 6:16
  • i have edited my question ,please you can show me the code since i am new to Magento, Thank you for your help – Hardik Radadiya Aug 19 '19 at 6:36
  • Yea, and in "Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection" file that you extends there is a _initSelect() function, so you can try to extends this function by creating in your Collection.php file upper function – lama377 Aug 19 '19 at 7:03
  • i have done as you said i got this error "SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous, query was: SELECT main_table.*, st.con_id, st.address, st.zipcode, st.country FROM my_module_contact AS main_table LEFT JOIN my_module_contact_add AS st ON main_table.id = st.con_id WHERE (id = '56')" – Hardik Radadiya Aug 19 '19 at 10:03
0

In Your

Vendor\ModuleName\Model\ResourceModel\ModuleName\Collection.php

Do Something Like That

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected $_idFieldName = 'entity_id';
    /**
     * Collection constructor
     *
     *
     * @param  EntityFactoryInterface $entityFactory
     * @param  LoggerInterface $logger
     * @param  FetchStrategyInterface $fetchStrategy
     * @param  ManagerInterface $eventManager
     * @param  StoreManagerInterface $storeManager
     * @param  AdapterInterface $connection
     * @param  AbstractDb $resource
     */
    public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        $this->_init('My\Module\Model\Save', 'My\Module\Model\ResourceModel\Save');
        $this->storeManager = $storeManager;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
    }
    /**
     *
     *  Filtering Data By Query on the Basis of Filtering Criteria
     *
     *
     */

    protected function _initSelect()
    {
        parent::_initSelect();
        $this->getSelect()->where('main_table.reserved_order_id NOT IN (SELECT sales_order.increment_id  FROM sales_order) ');
    }
}
  • getting error "Type Error occurred when creating object: My\Module\Model\ResourceModel\Save\Collection" – Hardik Radadiya Aug 19 '19 at 11:27
  • run php bin\magento setup:di:compile – Waqar Ali Aug 19 '19 at 11:33
  • yes its working now but its not showing data from the different table – Hardik Radadiya Aug 19 '19 at 11:43
  • is there any relation between both of your table?? – Waqar Ali Aug 19 '19 at 11:44
  • yes i have assigned foreign key in second table – Hardik Radadiya Aug 19 '19 at 11:44
0
 <?php
 namespace My\Module\Model\Save;
 use My\Module\Model\ResourceModel\Save\CollectionFactory;
 class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
 {
/**
 * @param string $name
 * @param string $primaryFieldName
 * @param string $requestFieldName
 * @param CollectionFactory $contactCollectionFactory
 * @param array $meta
 * @param array $data
 */

public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    CollectionFactory $contactCollectionFactory,
    \Magento\Framework\App\Request\Http $request,
    array $meta = [],
    array $data = []
) {
    $this->request = $request;
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    $collection = $contactCollectionFactory->create();

     $collection->getSelect()
            ->joinLeft(
                ['ca'=>'my_module_contact_add'],
                "ca.con_id = main_table.id",
                [
                'con_id' => 'ca.con_id',
                'address' => 'ca.address',
                'zipcode' => 'ca.zipcode',
                'country' => 'ca.country',
                ]
            );

    $this->collection = $collection;   


}

public function getData()
{
    if (isset($this->loadedData)) {

        return $this->loadedData;
    }
    $items = $this->collection->getItems();
    $this->loadedData = array();

    /** @var Customer $customer */
    foreach ($items as $contact) {
        // notre fieldset s'apelle "contact" d'ou ce tableau pour que magento puisse retrouver ses datas :
        $this->loadedData[$contact->getId()]['contact'] = $contact->getData();

    }

    return $this->loadedData;

}
}

this way i join my table to get data

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.