0

I want to delete an entry in my custom module table, in a similar way to this select:

    $select = $this->getConnection()->select()->from(
        ['*' => $this->getMainTable()]
    )->where(
        'quote_ref = ?',
        $quote_ref
    );

    $data = $this->getConnection()->fetchOne($select);

I am using the following code:

    public function deleteByRef($quote_ref = null)
    {
        $delete = $this->getConnection()->delete();
}

And getting the following error:

1 exception(s):

Exception #0 (Exception): Warning: Missing argument 1 for Zend_Db_Adapter_Abstract::delete(), called in C:\laragon\www\websitestore\app\code\Website\Quote\Model\ResourceModel\Data.php on line 43 and defined in C:\laragon\www\websitestore\vendor\magento\zendframework1\library\Zend\Db\Adapter\Abstract.php on line 650

How can I pass parameters to the delete method?

2 Answers 2

3

Here is what worked for me:

$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$myTable = $resource->getTableName('mytable');

$connection->delete(
    $myTable,
    ['order_id = ?' => 10000]
);
return;

Hope it helps.

5

If we take a look:

vendor/magento/framework/DB/Adapter/AdapterInterface.php

    /**
     * Deletes table rows based on a WHERE clause.
     *
     * @param  mixed        $table The table to update.
     * @param  mixed        $where DELETE WHERE clause(s).
     * @return int          The number of affected rows.
     */
    public function delete($table, $where = '');

As we can see, delete method needs at least one argument. In our case, we can try:

 $this->getConnection()->delete(
                $this->getMainTable(),
                ['quote_ref = ?', $quote_ref]
            );
2
  • I got an error, Exception #0 (Zend_Db_Statement_Exception): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound, query was: DELETE FROM ghm_udropship_vendor_shipping WHERE (vendor_id = ? ) AND (1). Can you please let me know. Commented Jun 6, 2017 at 8:00
  • You need to have the array key quote_ref = ? and the value $quote_ref in this example. Otherwise it treats the array as a list of different where clauses Commented Jan 18, 2021 at 23:07

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.