3

Does doctrine 2 support some kind of "select into"-syntax?

In pure MySQL it would be something like this:

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

I checked the manual, yet without sucess.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
herrjeh42
  • 2,782
  • 4
  • 35
  • 47

1 Answers1

9

I am refering to Doctrine in version 2.3.1: In this version you can get the current connection to the database from ORM/EntityManager. There is only one connection (this was different in Doctrine 1). Here is what worked for me for the insert-select-task:

$db = $this->_em->getConnection();
$query = "INSERT INTO table2 (myfield) SELECT table1.myfield FROM table1 WHERE table1.id < 1000";
$stmt = $db->prepare($query);
$params = array();
$stmt->execute($params);

There is also an interesting answer here: Using Raw SQL with Doctrine with an code example how to check "raw sql" for PDO compatiblity.

Community
  • 1
  • 1
herrjeh42
  • 2,782
  • 4
  • 35
  • 47
  • i have the same problem and my query was without this prepare() property. but when i add it it gave me this error FatalErrorException: Error: Call to undefined method Doctrine\ORM\Query::prepare() in controller – numerah Jul 17 '14 at 10:11