Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-hyperlocal-marketplace-guide

    Join two table and create grid in admin section using ui component in magento2

    Here we learn how to Join two table and create grid in admin section using ui component in magento2

    For create grid you can follow  our previous post how to create grid and insert data

    For join you need to edit following files
    app/code/Webkul/Grid/etc/di.xml //app/code/NameSpace/ModuleName/etc/di.xml
    app/code/Webkul/Grid/Model/ResourceModel/Grid/Collection.php

    //app/code/NameSpace/ModuleName/Model/ResourceModel/Grid/Collection.php

    # For join we need to update di.xml file as following

    2# Now we update app/code/Webkul/Grid/Model/ResourceModel/Grid/Collection.php
    folder structure app/code/NameSpace/ModuleName/Model/ResourceModel/ModelName/Collection.php


    3# now we create Collection.php in app/code/Webkul/Grid/Model/ResourceModel/Grid/Grid
    path structure : app/code/NameSpace/ModuleName/Model/ResourceModel/ModelName/Grid


    # now you can add 2nd table columns in grid_record_grid_list.xml in  app/code/Webkul/Grid/view/adminhtml/ui_component folder and then 2nd table column include in grid.

    Thanks

    . . .

    Comments (2)

    Add Your Comment

  • rvl
    Thanks for the comprehensive examples , there are not many magento2 documentation with examles available, so this helps a lot.

    I have however some suggestions. Being new to Magento and PHP with DI and namespaces and so on, it is very confusing that in the above example you have named your module Grid, and the Magento directory structure and namespace as part of Magento2 also is named Grid.

    You end up with things like WebkulGridModelResourceModelGridGridGrid.php

    At least name *your* Module MyGrid then, so it stays clear without having to resort to things like explaining the namespace to directory structure mappings everytime. It’s very confusing, especially when starting to find out how it works.

    But again, thanks for the blog-posts, it helps a lot.

  • Jakob Meissner
    Displaying the joined colums works fine. However, filtering on the joined columns does not work. Do you hav a solution for this?

    Turns out it doesn’t work when the column names in the grid contain dots, eg which my coulumns had because the sql field name are ambiguous.

    But: If you replace the dots with underscore, auto filtering also doesn’t work because the column than can not be found in sql query. Fix for this is explained here: http://magento.stackexchange.com/questions/126207/magento-2-custom-admin-grid-field-error-when-sorting-or-filtering.

    If someone has a simple solution I will be glad to hear.

  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close