3

I created an admin grid using reference https://ranasohel.me/2014/04/20/creating-magento2-adminhtml-grid/.

Now I want to apply addFieldToFilter() of my collection in the grid.

How can I do this? Because Magento 2 use ui_component for the grid in XML.

1 Answer 1

3

If you need to create join in any grid then you need to existing grid collection. For this example, the grid has own collection. So you can add following code to grid collection.

File: SR/Weblog/Model/ResourceModel/BlogPosts/Grid/Collection.php

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

    $this->join(
        [$this->getTable('second_table_name')],
        'main_table.blogpost_id = '.$this->getTable('second_table_name').'.blogpost_id',
        array()
    );

    return $this;
}

Also, you can use:

public function addFieldToFilter($field, $condition = null)
{
}
4
  • magento.stackexchange.com/q/117352/31354. I am confused in this link question.plz need suggestion Commented May 28, 2016 at 6:27
  • I am getting a:4:{i:0;s:366:"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'entity_id' in where clause is ambiguous, query was: SELECT COUNT(*) FROM sales_order` AS main_table INNER JOIN sales_order_address AS secondTable ON main_table.entity_id = secondTable.parent_id AND secondTable.address_type="billing" WHERE (entity_id IN('1'))"` Commented Feb 1, 2017 at 8:56
  • @zedBlackbeard Please post new question regarding your problem. It will be good to help with.
    – Sohel Rana
    Commented Feb 1, 2017 at 9:37
  • I have fixed that by overriding the fields name using _construct function. It is working now. Thanks for your response. Commented Feb 1, 2017 at 9:47

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.