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

After updating Phalcon from 3.0.0 to 3.2.2 modelsManager queries stopped working

The code doesn't work after the update:


    public static function increaseViews($entity)
    {
        $query = "UPDATE \Frontend\Entity SET views = views + 1 WHERE id = :id:";
        return $entity->getModelsManager()->executeQuery($query, ['id' => $entity->id]);
    }

But this one works fine:

    public static function increaseViews($entity)
    {
        $query = "UPDATE \Frontend\Entity SET views = views + 1 WHERE id = :id:";
        return $entity->getModelsManager()->createQuery( $query )->execute(['id' => $entity->id]);
    }

What is the difference? Or just the first code snippet doesn't work on newer versions?

We need more info, like what exactly happens?



13.8k

What kind of error are you getting?



1.5k

Both code snippets don't work on the production server.

That's another side of my problem, is that there are no errors. I opened up the log files to see the raw mySQL queries. And this query is just not being generated.

The weird thing is that among 10 identical table entries, increment triggers only for 1 entry. I.e. this query is being generated for one particular table entry. Although the rest are the same.

Downgraded to 2.1.0 RC1, and it works fine.

2.1.0? Well, that's far from good decision. You should have downgraded to 3.0.0 which was working for you.



1.5k

How to downgrade to 3.0.0? I managed to do it only to 3.0.8

git clone --depth=1 git://github.com/phalcon/cphalcon.git --branch 3.0.x

In 3.0.8 version the code didn't work as well. Any advice on how to downgrade to 3.0.0 would be greatly appreciated. And what should I do with my queries?



1.5k

This a code doesn't work too :(


        //Phalcon ver: 3.2.2
        $query = "UPDATE Entity SET views = views + 1 WHERE id = ?";
        return $this->db->query($query, [$entity->id,]);
edited Sep '17

Then if this last code doesn't work it's problem with your database somehow. What happens exactly? Check logs, maybe you just have pdoexception?

edited Sep '17

I checked your code and it works without any issue on phalcon 3.0.4 and 3.2.0



1.5k

I'm sorry, I was wrong in the last post, it works for me too.

This a code doesn't work too :(

Now I did the following: downgrade to 3.0.0: https://github.com/phalcon/cphalcon/releases?after=v3.2.0

and posted the code snippet, that work perfectly fine:


        $article = new Article();
        $article->title = "test";
        $article->views = 0;

        if ($article->save() == false) {
            foreach ($article->getMessages() as $message) {
                var_dump($message);
            }
            return false;
        }

        $query = "UPDATE \Models\Article SET views = views + 1 WHERE id = :id:";
        $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);
        $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);
        $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);

        $art = Article::findFirst( $article->id );
        var_dump( $art->views ); //3

Upgraded to 3.0.1 version, and the code stops working.

So I can't really upgrade higher than 3.0.0. And I had to downgrade back to that version.

What should I do? Maybe someone can advise me on how to catch the exception?

edited Sep '17

just put this code into try catch, or just check logs. I checked this on 3.2.0 and works fine. For sure nothing was changed between 3.0.0 and 3.0.1 to break this.



1.5k
edited Sep '17

At the moment "try catch" doesn't catch the error because the result is not complete. I.e. if the version is 3.0.0, it's gonna show = 3:


    error_reporting(E_ALL & E_NOTICE);
    try{
            $query = "UPDATE \Models\Article SET views = views + 1 WHERE id = :id:";
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   //1
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   //2
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   //3

            $art = Article::findFirst( $article->id );
            var_dump( $art->views );            // 3
    }catch( \PDOException $e ){
            var_dump( $e->getMessage() );
    }

But after the upgrade to 3.0.1 the code gives me = 1, i.e. try catch will work normally:


    error_reporting(E_ALL & E_NOTICE);
    try{
            $query = "UPDATE \Models\Article SET views = views + 1 WHERE id = :id:";
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   // 1 
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   // 1
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   // 1

            $art = Article::findFirst( $article->id );
            var_dump( $art->views );            // 1
    }catch( \PDOException $e ){
            var_dump( $e->getMessage() );
    }

And another really weird thing is that if I run the mixed code PDO with Phalcon's ORM in 3.0.1 version, the code works fine:


    error_reporting(E_ALL & E_NOTICE);
    try{
            $query = "UPDATE Article SET views = views + 1 WHERE id = ?";
            \Phalcon\DI::getDefault()['db']->query($query, [$article->id]);             // 1
            \Phalcon\DI::getDefault()['db']->query($query, [$article->id]);         // 2

            $query = "UPDATE \Models\Article SET views = views + 1 WHERE id = :id:";
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   // 3
            $article->getModelsManager()->createQuery( $query )->execute(['id' => $article->id]);   // 4

            $art = Article::findFirst( $article->id );
            var_dump( $art->views );            // 4
    }catch( \PDOException $e ){
            var_dump( $e->getMessage() );
    }


1.5k
edited Sep '17

I think I found the problem. Add this to the model's initialize function.


    public function initialize(){
        $this->useDynamicUpdate(true);
    }

Do u have same result?