Doctrine 2 DBAL Expressions in Update or Insert Methods

I like the convenience methods for processing data requests $ conn-> insert () and $ conn-> update () in the 2 DBAL doctrine, since insert / update values ​​can be passed as an associative array. But how can I pass a NULL value, a MySQL function, or other expressions as a value?

eg:

/* $conn is a \Doctrine\DBAL\Connection object */ $conn->update('person', array('phone' => 'NULL'), array('id' => 1)); $conn->update('person', array('lastlogin' => 'NOW()'), array('id' => 1)); $conn->update('person', array('visit' => 'visit + 1'), array('id' => 1)); 

These function calls would create prepared expressions like

 UPDATE person SET phone = ? WHERE id = ? 

and therefore, the values ​​will be treated as strings. Is there any way to do this work using this technique?

+7
php mysql doctrine doctrine2 dbal
source share
1 answer

There is an optional $types argument, the default is to use an empty array:

  public function update($tableExpression, array $data, array $identifier, array $types = array()) 

The $types array may contain PDO type constants , which will change the way that the corresponding $data values ​​are processed.

So, I would try:

 $conn->update( 'person', // $tableExpression array('phone' => null), // $data array('id' => 1), // $identifier array('phone' => \PDO::PARAM_NULL) // $types ); 
+2
source share

All Articles