How to modernize the column in the table from another table’s column in Magento 2

Sometimes you want to modernize the column in the table from another table’s column in Magento 2.

You can use “update from select “function to do it

The following example for resource model

$connection = $this->getConnection();

$select = $connection->select()

 ->from(

 ['main_table' => $this->getResource()->getMainTable()]

  )

  ->joinLeft(['join_table'=>$this->getTable('join_table')],

  "main_table.id = join_table.id",

 array('name' => 'name'));

$query = $connection->updateFromSelect(

$select,

 ['main_table' => $this->getResource()->getMainTable()]

);

$connection->query($query);

This is the query after the code is contribute

UPDATE `maintable` AS `maintable`

INNER JOIN `maintable`

LEFT JOIN `'join_table'` AS `'join_table'` ON main_table.id = 'join_table'.id

SET `'main_table'`.`name` = `'join_table'`.`name`

I hope the following steps are the smallest process for you to modernize the column in the table from another table in Magento 2.

Last Update: April 9, 2018  

February 14, 2018   126   Nandini R    Tips And Tricks    
Total 2 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Google+
http://www.webnexs.com/blog/kb/modernize-column-table-from-another-tables-column
Twitter
LinkedIn