ZenDB question
11 posts by 3 authors in: Forums > CMS Builder
Last Post: July 25 (RSS)
Hi Jeff,
Here's a few ways to drop a column from MySQL with ZenDB:
// option 1
$fullTable = DB::getFullTable('test_table'); // add prefix, e.g., cms_test_table
$columnName = "test_column";
DB::query("ALTER TABLE $fullTable DROP COLUMN $columnName");
// option 2
DB::query("ALTER TABLE :_test_table DROP COLUMN test_column");
// option 3
DB::query("ALTER TABLE :tableName DROP COLUMN :columnName",[
':tableName' => DB::raw(DB::getFullTable('test_table')),
':columnName' => DB::raw('test_column'),
]);
A few things to note:
- If you want/need error handling, the current version returns errors as $result->error, but the next version will throw Exceptions that you can try/catch if needed
- Anything passed as a parameter (which we're not doing in the above code) gets quoted and escaped. So if you don't want that, such as when manipulating the MySQL columns, you need to insert it directly or use DB::raw() on a parameter value to let ZenDB know you're passing raw SQL.
- :_ is shorthand to insert the current table prefix, e.g., cms_
Let me know any feedback or questions, thanks.
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com