Hydrahon icon indicating copy to clipboard operation
Hydrahon copied to clipboard

Execute raw queries

Open neto737 opened this issue 4 years ago • 1 comments

Hi,

Is there any way to run raw queries using Hydrahon?

I want to truncate a table, but I haven't found a function in Hydrahon, and also haven't found a way to run a raw query, is it possible?

Also, is there any way to create tables using Hydrahon? It could be using raw queries as I wanna do to truncate a table.

Thank you in advance.

neto737 avatar Nov 21 '20 21:11 neto737

I've just found that it's possible to truncate using:

$h->table('table_name')->truncate()->execute();

Also found that it's possible to drop a table using:

$h->table('table_name')->drop()->execute();

But I haven't found anything to add/remove an column from an existing table, and also haven't found anything to create a new table.

neto737 avatar Nov 22 '20 20:11 neto737

Hello - I just want to say that this lib is for data manipulation (insert/select/...) AFAIK, but for structure manipulation you could use Phinx (it's migration lib, but it may work for you in a different way too): https://book.cakephp.org/phinx/0/en/index.html

marek-hanzal avatar Nov 15 '22 10:11 marek-hanzal

@neto737 is completely right about the truncate and drop operations.

Hydrahon is query builder not an ORM layer, so running raw queries completely depends on your implementation. If you are using PDO, just use the PDO objects to run your raw queries:

$connection = new PDO('mysql:host=localhost;dbname=my_database;charset=utf8', 'username', 'password');

// create a new mysql query builder
$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
{
    // your implemenation
});

$connection->prepare('delete from something where x = ?')->execute([$myParam]);

mario-deluna avatar Nov 15 '22 14:11 mario-deluna