XF 2.1 How to remove multiple columns from a table?

Matt C.

Well-known member
Can someone tell me what the equivalent of insertBulk is when removing columns from a table?

Thank you very much.
 
insertBulk inserts rows of data, so I assume you mean rows rather than columns... In case you did mean columns then you should be using the schema manager for that which has a dropColumns method.

But aside from that, there doesn't need to be a bulk method for deleting rows from a table, because the deletes work like a select query in that you just give the relevant conditions/limits and based on that it will delete as many rows as it matches.

So you'd be looking at either:
PHP:
$db->query('
    DELETE FROM table
    WHERE column = ?
', 'something');
Or:
PHP:
$db->delete('table', 'column = ?', 'something');
 
insertBulk inserts rows of data, so I assume you mean rows rather than columns... In case you did mean columns then you should be using the schema manager for that which has a dropColumns method.

But aside from that, there doesn't need to be a bulk method for deleting rows from a table, because the deletes work like a select query in that you just give the relevant conditions/limits and based on that it will delete as many rows as it matches.

So you'd be looking at either:
PHP:
$db->query('
    DELETE FROM table
    WHERE column = ?
', 'something');
Or:
PHP:
$db->delete('table', 'column = ?', 'something');

Yes I meant rows, sorry. Thank you Chris.
 
@Chris D, I get the following error when trying to uninstall my add-on.

Code:
XF\Db\InvalidQueryException: MySQL statement prepare error [1064]: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'column = field_id' at line 1 in src\XF\Db\AbstractStatement.php at line 217

XF\Db\AbstractStatement->getException() in src\XF\Db\Mysqli\Statement.php at line 196
XF\Db\Mysqli\Statement->getException() in src\XF\Db\Mysqli\Statement.php at line 39
XF\Db\Mysqli\Statement->prepare() in src\XF\Db\Mysqli\Statement.php at line 54
XF\Db\Mysqli\Statement->execute() in src\XF\Db\AbstractAdapter.php at line 89
XF\Db\AbstractAdapter->query() in src\XF\Db\AbstractAdapter.php at line 243
XF\Db\AbstractAdapter->delete() in src\addons\AH\GamerProfiles\Setup.php at line 114
AH\GamerProfiles\Setup->uninstall() in src\XF\Admin\Controller\AddOn.php at line 635
XF\Admin\Controller\AddOn->actionUninstall() in src\XF\Mvc\Dispatcher.php at line 321
XF\Mvc\Dispatcher->dispatchClass() in src\XF\Mvc\Dispatcher.php at line 248
XF\Mvc\Dispatcher->dispatchFromMatch() in src\XF\Mvc\Dispatcher.php at line 100
XF\Mvc\Dispatcher->dispatchLoop() in src\XF\Mvc\Dispatcher.php at line 50
XF\Mvc\Dispatcher->run() in src\XF\App.php at line 2155
XF\App->run() in src\XF.php at line 389
XF::runApp() in admin.php at line 13

Here is the line:
Code:
$this->db()->delete('xf_user_field','column = field_id', 'ah_playstation');

Thank you very much!
 
It must be field_id = ?, not column = field_id if you want to delete rows where field_id has a certain value.
 
Last edited:
Back
Top Bottom