cancel
Showing results for 
Search instead for 
Did you mean: 

How to delete a record from a custom table using where?

How to delete a record from a custom table using where?

I want to delete a record from a custom table using where, but im unsure how to write the paramters correctly.

Which is the correct way Code 1 or Code 2 ?

 

protected $resourceConnection;

public function __construct(ResourceConnection $resourceConnection)
{
$this->resourceConnection = $resourceConnection;
}

$table = $this->resourceConnection->getTableName("custom_table");
Code 1
$this
->resourceConnection->getConnection()->delete($table,["entity_id = $productID"]);
Code 2
$this
->resourceConnection->getConnection()->delete($table,["entity_id = ?", $productID]);

 

5 REPLIES 5

Re: How to delete a record from a custom table using where?

Hi,

you can use below code to delete a customer from "customer_entity" table.

 

$customerId = 5;
$connection= $this->resourceConnection->getConnection();
$customerTable = $this->resourceConnection->getTableName('customer_entity');
$sql = "DELETE  FROM $customerTable WHERE entity_id=$customerId";         
$connection->query($sql);

Thanks

Re: How to delete a record from a custom table using where?

I need an answer using the delete function, not the raw sql query.

Re: How to delete a record from a custom table using where?

I think the best way just follow Magento standard. For example just check Cms magento module.

So you have a new module app/code/MyCompany/MyModule

Model/Comment.php

namespace MyCompany\MyModule\Model;

class Comment extends \Magento\Framework\Model\AbstractModel {

    /**
     * @var string
     */
    protected $_cacheTag = 'customer_comment';

    /**
     * @var string
     */
    protected $_eventPrefix = 'customer_comment';

    /**
     * @return void
     */
    protected function _construct()
    {        $this->_init(\MyCompany\MyModule\Model\ResourceModel\Comment::class);
    }

}

Model/ResourceModel/Comment.php

namespace MyCompany\MyModule\Model\ResourceModel;

class Comment extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb {

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {        $this->_init('you_custom_table_name', 'comment_id');
    }

}

Controller/Comment/Delete.php

namespace MyCompany\MyModule\Controller\Comment;

use MyCompany\MyModule\Model\CommentFactory;

class Delete extends \Magento\Framework\App\Action\Action {

    /**
     * @var CommentFactory
     */
    protected $_commentFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param CommentFactory $commentFactory
     */
    public function __construct(        \Magento\Framework\App\Action\Context $context,
        CommentFactory $commentFactory
    ) {        $this->_commentFactory = $commentFactory;        parent::__construct($context);
    }

    public function execute() 
    {        $id = $this->getRequest()->getParam('comment_id');
        try {            $model = $this->_commentFactory->create();            $model->load($id);            $model->delete();
        } catch (\Exception $e) {            $this->messageManager->addError($e->getMessage());
        }
    }

}

Re: How to delete a record from a custom table using where?

Re: How to delete a record from a custom table using where?

Well, both ways will get the job done, but in this case the usage of CODE 2 is considered safe as php doc state:

where statement.png