Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine executeUpdate array parameter

How can I do update in doctrine, with array, so I don't do for loop for each time (I just want to do 1 call to database)

$myarray = [1, 2, 3];
$sql = "UPDATE `mytable` SET is_processing = :is_processing, end_time=NOW() WHERE id = :id";

$result = $this->connection->executeUpdate(
    $sql,
    array(
        'is_processing' => false,
        'id' => $myarray // This is unknown number amount of array
    )
);

What I'm trying to achieve is: it should update the table with field is_processing = false, endTime become current time, where id = whatever the array point to

like image 220
Harts Avatar asked Apr 02 '16 23:04

Harts


1 Answers

Use IN clause in your query.

UPDATE `mytable` SET is_processing = :is_processing, end_time=NOW() WHERE id IN(:ids)

then

$result = $this->connection->executeUpdate(
    $sql,
    array(
        'is_processing' => false,
        'ids' => [3, 25]
    ),
    array(
        'ids' => \Doctrine\DBAL\Connection::PARAM_INT_ARRAY
    )
);
like image 117
Federkun Avatar answered Oct 12 '22 22:10

Federkun