0

We want to insert data in database table. My site is in magento 2. Any one help me for insert data in database table.

namespace Agtech\Productstockupdate\Block\Index;
class Index extends \Magento\Framework\View\Element\Template {
protected $productFactory;
public function __construct(
 \Magento\Catalog\Block\Product\Context $context,
 \Magento\Catalog\Model\ProductFactory $productFactory, 
 array $data = []) {
 $this->productFactory = $productFactory;
 parent::__construct($context, $data);
}
protected function _prepareLayout()
{
 return parent::_prepareLayout();
}
public function updateStock($product_sku,$product_QTY)
{
 $product = $this->productFactory->create()->load($productId);
 if($product->getStatus() == 2){ // 2 => Disable , 1 => Enable
 $product->setStatus(1);
 if($product->getStatus() == 1){
 $product->setStockData([
 'is_in_stock' => 1, 
 'qty' => $productQty 
 ]);
 $product->setQuantityAndStockStatus([
 'qty' => $productQty,
 'is_in_stock' => 1
 ]); 
 }
 $product->setStatus(2);
 try {
 $product->save(); 
 echo 'Quantity update of SKU => '.$product->getSku().'<br/>'; 
 } catch (Exception $e) {
 echo $e->getException();
 }
 }else{
 $product->setStockData([
 'is_in_stock' => 1,
 'qty' => $productQty
 ]);
 $product->setQuantityAndStockStatus([
 'qty' => $productQty,
 'is_in_stock' => 1
 ]); 
 try {
 $product->save(); 
 echo 'Quantity update of SKU => '.$product->getSku().'<br/>'; 
 } catch (Exception $e) {
 echo $e->getException();
 }
 }
}
}
asked Jul 4, 2019 at 13:40
3
  • In which table you want to insert that data? Commented Jul 4, 2019 at 13:59
  • Any new table create in database and insert in it. we want two columns. Commented Jul 4, 2019 at 14:01
  • Magento uses CRUD models to insert data, you can read up on it here - mageplaza.com/magento-2-module-development/… Commented Jul 4, 2019 at 14:02

1 Answer 1

1

We can insert the data by following way:

namespace YourNamespace\YourModule\Controller;
 
use Magento\Framework\App\Action\Context;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\App\ResourceConnection;
 
class YourController extends \Magento\Framework\App\Action\Action
{
 /**
 * @var \Magento\Framework\Message\ManagerInterface
 */
 protected $messageManager;
 
 /**
 * @var \Magento\Framework\App\ResourceConnection $resource
 */
 protected $resource;
 
 protected $connection;
 
 public function __construct(
 Context $context,
 ManagerInterface $messageManager,
 ...
 ...
 ResourceConnection $resource
 )
 {
 $this->messageManager = $messageManager;
 ...
 ...
 $this->resource = $resource;
 $this->connection = $resource->getConnection();
 
 parent::__construct($context);
 }
 public function execute() {
 .....................
 .....................
 $this->yourCustomFunction();
 .....................
 .....................
 }
 
 public function insertMultiple($table, $data)
 {
 try {
 $tableName = $this->resource->getTableName($table);
 return $this->connection->insertMultiple($tableName, $data);
 } catch (\Exception $e) {
 $this->messageManager->addException($e, __('Cannot insert data.'));
 }
 }
 
 public function yourCustomFunction()
 {
 try {
 $yourData = [
 ['id' => 1, 'name' => 'John', 'age' => 22],
 ['id' => 2, 'name' => 'Mary', 'age' => 23],
 ['id' => 3, 'name' => 'Sam', 'age' => 24]
 ];
 
 $tableName = 'your_table_name';
 $this->insertMultiple($tableName, $yourData);
 
 $this->messageManager->addSuccess( __('Successfully inserted data.') );
 } catch (Exception $e) {
 $this->messageManager->addException($e, __('Cannot save data.'));
 }
 }
}

Hope this helps. Thanks!

answered Jul 4, 2019 at 13:51
8
  • @Anil Here you want to save the data by product id or product sku? Commented Jul 4, 2019 at 14:03
  • here is our custom data; not magento default. If you want any name change so dont worry. Commented Jul 4, 2019 at 14:04
  • @Anil If you want to insert data into custom table then you can use my way, please try it. Commented Jul 4, 2019 at 14:07
  • Is these use with Block file ? Commented Jul 4, 2019 at 14:19
  • No, here I am doing this with controller file. Commented Jul 5, 2019 at 10:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.