on my magento I need to run a custom query to fix some data. I’m trying to write my queries using Magento standard way as much as possible. The code is pretty simple: first I check if there are any rows in the table and, if there are, then I’ll delete them:
/* MagentoFrameworkAppResourceConnection $conn */
$tablename = $conn->getTableName($table);
$query = $conn->select()->from(['main_table' => $tablename], ['*'])
->where('main_table.store_id > ?', self::STORE_ID)
->where('main_table.entity_id NOT IN (?)', self::IDS_TO_EXCLUDE)
;
$results = $conn->fetchAll($query);
if(count($results) > 0) {
$bind = [
$conn->quoteInto('main_table.store_id > ?', self::STORE_ID),
$conn->quoteInto('main_table.entity_id NOT IN (?)', self::IDS_TO_EXCLUDE)
];
$rows = $conn->delete($tablename, $bind);
}
This code works. My problem is that the where statements in the select query and the bind statements in the delete query are the same, so basically I’m writing the same code twice. I’d like to avoid the repetition by using only the bind, but I don’t know how to replace the where statements with the bind statements. Everything I’ve tried so far throws the error:
An error encountered: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens, query was: SELECT `main_table`.* FROM `xxx` AS `main_table`
Is there a way to use the bind array in the select statement?