Is there a way to execute a SQL String as a query in Zend Framework 2?
I have a string like that:
$sql = "SELECT * FROM testTable WHERE myColumn = 5"
now I want to execute this string directly.
Is there a way to execute a SQL String as a query in Zend Framework 2?
I have a string like that:
$sql = "SELECT * FROM testTable WHERE myColumn = 5"
now I want to execute this string directly.
If you are using tableGateway, you can run your raw SQL query using this statement,
where $sql pertains to your raw query. This can be useful for queries that do not have native ZF2 counterpart like TRUNCATE / INSERT SELECT statements.
If you have
EntityManager
$em on your hands, you can do something like this:I think this way to execute complex queries is best for Zend. But may be I'm not very smart in Zend still. Will glad to see if it helps to someone.
S. docu: Zend\Db → Zend\Db\Sql
Just pass the sql string to your db adapter like this:
And if you want to pass parameters:
EDIT: Please note that the query method does not always returns a resultset. When its a resultset producing query(
SELECT
) it returns a\Zend\Db\ResultSet\ResultSet
otherwise(INSERT
,UPDATE
,DELETE
, ...) it will return a\Zend\Db\Adapter\Driver\ResultInterface
.And when you leave the second Parameter empty you will get a
\Zend\Db\Adapter\Driver\StatementInterface
which you can execute.