We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

batch update query not work

This query work correctly when user that user.rank is 1 is not included in $userIds .but, this query not work when user that user.rank is 1 is included in $userIds .

so what's happen!?.

phalcon's version is 3.1.

        $userIds = [1,2,3,4];
        $increasedCount = 1;

        var_dump($increasedCount);
        switch ($increasedCount) {
            case $increasedCount > 0:
                $countSql = "+ " . $increasedCount;
                break;
            case $increasedCount < 0:
                $countSql = "- " . abs($increasedCount);
                break;
            default:
                return false;
        }

        $user = new UserEntity();

        try {
            $phql = "UPDATE Application\Models\Entities\User SET rank = rank $countSql WHERE id IN (" . implode(",", $userIds) . ")";
            return $user->getModelsManager()->executeQuery($phql);
        } catch (Exception $e) {
            return false;
        }
edited Nov '17

What does "it does not work" exactly mean? Does it throw an exception? Does it not perform the expected operation?

You can also omit the $countSql and the switch logic, SQL addition inherently handles signs.

$userIds = [1,2,3,4];
$increasedCount = 1;
if($increasedCount === 0) {
    return false;
}
$user = new UserEntity();
try {
    $phql = "UPDATE Application\Models\Entities\User SET rank = rank + :count:  WHERE id IN (" . implode(",", $userIds) . ")";
    return $user->getModelsManager()->executeQuery($phql, ['count'=>$increasedCount]);
} catch (Exception $e) {
    return false;
}


2.5k
edited Nov '17

Thank you for your answer.

What does "it does not work" exactly mean?

This means that query return success result. but , user.rank that user.rank is 1 was'nt added.

So, I changed code as below while I expected to go well.

But , the result is same as the previous one.

Consequently,,,, I'm in a puzzle...

$userIds = [1,2,3,4];
$increasedCount = 1;
if($increasedCount === 0) {
    return false;
}
$user = new UserEntity();
try {
    $phql = "UPDATE Application\Models\Entities\User SET rank = rank + 1  WHERE id IN (" . implode(",", $userIds) . ")";
    return $user->getModelsManager()->executeQuery($phql);
} catch (Exception $e) {
    return false;
}


2.5k

I feel like that user.rank is recognized as boolean when user.rank is 1. and user.rank that user.rank is'nt 1 is recognized as int

UPDATE Application\Models\Entities\User SET rank = rank + 1 WHERE id IN (" . implode(",", $userIds) . ")"