Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php magento 2中的更新功能不工作_Php_Magento2 - Fatal编程技术网

Php magento 2中的更新功能不工作

Php magento 2中的更新功能不工作,php,magento2,Php,Magento2,我想更新数据库中某个条目的状态。我试图执行更新查询,但它抛出httperror 500错误。该项目是在magento 2中完成的。这是我的密码 namespace Bridge\Batchcode\Controller\Adminhtml\Items; use \Magento\Framework\Model\ResourceModel\Db\AbstractDb; class Delete extends \Bridge\Batchcode\Controller\Adminhtml\Items

我想更新数据库中某个条目的状态。我试图执行更新查询,但它抛出
httperror 500
错误。该项目是在magento 2中完成的。这是我的密码

namespace Bridge\Batchcode\Controller\Adminhtml\Items;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Delete extends \Bridge\Batchcode\Controller\Adminhtml\Items{

public function execute()
{
    $id = $this->getRequest()->getParam('id');
    if ($id) {
        try {
            $model = $this->_objectManager->create('Bridge\Batchcode\Model\Items');
            $model->load($id);
            $productid = $model->getProductId();
            $prev_qty = $model->getBatchQty();


            $bind       = ['status' => 'Inactive'];
            $where      = ['id = ?' => (int)$id] ];
            $tableName  = $this->getConnection()->getTableName("batchcode_entity");

            $this->getConnection()->update($tableName, $bind, $where);

                $objectManager  = \Magento\Framework\App\ObjectManager::getInstance();
                $stockRegistry  = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
                $product        = $objectManager->get('Magento\Catalog\Model\Product')->load($productid);
                $stockitem      = $stockRegistry->getStockItem(
                                                            $product->getId(),
                                                            $product->getStore()->getWebsiteId()
                                                            );

            if ( ($stockitem->getId() > 0) && ($stockitem->getManageStock())) {
                $qty     = $stockitem->getQty();
                $qty_new = $qty-$prev_qty;
                $stockitem->setQty($qty_new);
                $stockitem->save();
            }

            $this->messageManager->addSuccess(__('You deleted the item.'));
            $this->_redirect('bridge_batchcode/*/');
            return;

        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addError(
                __('We can\'t delete item right now. Please review the log and try again.')
            );
            $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
            $this->_redirect('bridge_batchcode/*/edit', ['id' => $this->getRequest()->getParam('id')]);
            return;
        }
    }
    $this->messageManager->addError(__('We can\'t find a item to delete.'));
    $this->_redirect('bridge_batchcode/*/');
  }
}

我不确定我是否遗漏了什么,因为我对magento是新手。我可以在magento中执行自定义查询吗?如果是,我该怎么做?

我认为您的问题在于如何使用$this->getConnection()。 这是一种方法,但我看不出你在哪里定义它。 您需要将此
\Magento\Framework\App\ResourceConnection$resource,
添加到构造函数中,然后创建getConnection()


我认为您的问题在于如何使用$this->getConnection()。 这是一种方法,但我看不出你在哪里定义它。 您需要将此
\Magento\Framework\App\ResourceConnection$resource,
添加到构造函数中,然后创建getConnection()

 protected $connection;

 public function __construct(

        \Magento\Framework\App\ResourceConnection $resource,

        Context $context,
        public function __construct(
        $this->_resource = $resource;

        parent::__construct($context, $data);
    }

  protected function getConnection()
  {
     if (!$this->connection) {
         $this->connection = $this->_resource->getConnection('core_write');
     }
     return $this->connection;
  }