Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2.0 native query without mapping

I am writing a tiny little migration script and i am only trying to update one attribute of one element. The Result i need has no Representation in the local Environment, so what i would need is a very simple SQL (here it is Oracle) handler that i can iterate over and get an array returned.

Is that possible with doctrine?

i.e. i would want to do this:

$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->execute($query)->iterate();
foreach ($iterator as $array) {
    // do something with an associative array
}

UPDATE / SOLUTION: With the Hint from Corbin i came up with this Solution which works pretty fine:

$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->query($query);
while (is_object($iterator) AND ($array = $iterator->fetch()) !== FALSE) {
        // do something with an associative array
}
like image 470
Andresch Serj Avatar asked Sep 09 '11 10:09

Andresch Serj


1 Answers

https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html#native-sql

If you want to do any mapping.

Another option would be to get the connection object from EntityManager::getConnection and operate on it.

It returns a Doctrine\DBAL\Connection which you should be able to work with. It has the typical fetchColumn fetchArray fetchAssoc so on.

like image 70
Corbin Avatar answered Nov 05 '22 21:11

Corbin