Reputation: 7734
I have following code in my cli application tasks (similar to controller behavior in main app). However i find sometimes when i execute this code i get error as below. Which is suggest something went wrong in the SQL statement. However when i checked and tested, even though its giving me an error db update is successful. So i wonder what is the root cause behind this issue.
Code to execute is
public function updateUserCountsAction(){
try{
$phql="UPDATE `user` u JOIN
(
SELECT COUNT(`user_to`) AS fav_count, user_to
FROM `users_favorite`
GROUP BY `user_to`
) uf
ON uf.user_to = u.user_id
SET u.fav_count = uf.fav_count;";
$user = new User();
$rs = new Phalcon\Mvc\Model\Resultset\Simple(null, $user, $user->getReadConnection()->query($phql));
}
catch(\Exception $e){
$this->error($e);
}
}
This is the error i get SOMETIMES ONLY, 50% of the times roughly. I wonder why ? but the execution of the SQL seems to update the DB even if i get this error.
========================================
2016-02-11 18:40:42:
PDOException: SQLSTATE[HY000]: General error
File=/var/www/vhosts/example.com/httpdocs/apps/cliend/tasks/CronTask.php
Line=240
#0 [internal function]: PDOStatement->fetchAll()
#1 [internal function]: Phalcon\Db\Result\Pdo->fetchAll()
#2 [internal function]: Phalcon\Mvc\Model\Resultset->__construct(Object(Phalcon\Db\Result\Pdo), NULL)
#3 /var/www/vhosts/example.com/httpdocs/apps/cliend/tasks/CronTask.php(240): Phalcon\Mvc\Model\Resultset\Simple->__construct(NULL, Object(LXY\Models\User), Object(Phalcon\Db\Result\Pdo))
#4 [internal function]: CronTask->updateUserCountsAction()
#5 [internal function]: Phalcon\Dispatcher->dispatch()
#6 /var/www/vhosts/example.com/httpdocs/apps/cliend/console.php(94): Phalcon\Cli\Console->handle(Array)
#7 {main}
Upvotes: 1
Views: 250
Reputation: 4980
I would blame your configuration of mysql_nd for ONLY 50% error rate, but first of all, try to not use Resultset. Update queries does not return values (1,2):
$user = new User();
$rs = $user->getWriteConnection()->query($phql);
Also use getWriteConnection()
once you are actually trying to write to DB.
If both does not help, inspect your mysql_nd configuration. Found HY000
error with properly performing script when having a dead slave configured in mine cluster.
Upvotes: 2