Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG?] After updating Phalcon from 3.0.0 to 3.0.1 or higher raw queries work incorrectly #13081

Closed
yesworld opened this issue Sep 22, 2017 · 10 comments
Assignees

Comments

@yesworld
Copy link

yesworld commented Sep 22, 2017

In 3.0.0 the code works fine:

$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

In 3.0.1 and higher results are different:

$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

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:

$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

I've found the parameter because of which the code gives me different results:

class Article extends \Phalcon\Mvc\Model{
    public $id;
    public $title;
    public $views = 0;

    public function initialize(){
       // $this->useDynamicUpdate(true);  //   <=== Try to uncomment this line
    }

    public function getSource()  {  return 'Article';  }
}

Should this parameter work exactly this way in newer versions or not?

Details

  • Phalcon version: 3.0.1 (php --ri phalcon)
  • PHP Version: PHP 5.5.9 (php -v)
  • Operating System: Linux version 3.19.0-32-generic
  • Installation type: Compiling from source
  • Server: Nginx | Apache
  • Other related info (Database, table schema): mySql
@yesworld yesworld changed the title After updating Phalcon from 3.0.0 to 3.0.1 or higher raw queries work incorrectly [BUG?] After updating Phalcon from 3.0.0 to 3.0.1 or higher raw queries work incorrectly Sep 25, 2017
@sergeyklay
Copy link
Contributor

@yesworld Did you try latest stable Phalcon? At least just for test

@yesworld
Copy link
Author

yesworld commented Sep 29, 2017

@sergeyklay yes,
First I updated to version 3.2.2. It doesn't work.
Then I downgraded to 2.1.0 RC1. It works.
Then tested on versions 3.0.8, 3.0.4 and 3.0.1. It doesn't work with this parameter too.

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

@sergeyklay
Copy link
Contributor

@Jurigag Could you please try to sort out?

@yesworld
Copy link
Author

Any news? @sergeyklay , @Jurigag

@Jurigag
Copy link
Contributor

Jurigag commented Nov 24, 2017

Can't reproduce on phalcon 3.0.2 and on phalcon 3.2.0, please provide steps to reproduce this issue. Are you sure that issue exists on latest phalcon version?

$di = new FactoryDefault();
$di->set(
    'db',
    function () {
        $adapter = new \Phalcon\Db\Adapter\Pdo\Mysql(
            [
                'host' => 'localhost',
                'username' => 'root',
                'password' => '',
                'dbname' => 'phalcon_test',
                'options' => [
                    PDO::ATTR_EMULATE_PREPARES => false,
                    PDO::ATTR_STRINGIFY_FETCHES => false,
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                ],
                'charset' => 'utf8',
            ]
        );

        return $adapter;
    }
);

class Users extends \Phalcon\Mvc\Model
{
    public $id;
    public $name;
    public $value=0;
}

var_dump(Users::findFirst(1)->value);
$query = "UPDATE Users SET value = value + 1 WHERE id = :id:";
$di->get('modelsManager')->createQuery($query)->execute(['id' => 1]);
$di->get('modelsManager')->createQuery($query)->execute(['id' => 1]);
$di->get('modelsManager')->createQuery($query)->execute(['id' => 1]);
var_dump(Users::findFirst(1)->value);

echo phpversion("phalcon");

Results:

/vagrant/www/test.php:67:int 1
/vagrant/www/test.php:72:int 4
3.2.0
/vagrant/www/test.php:56:int 1
/vagrant/www/test.php:61:int 4
3.0.2

@Jurigag
Copy link
Contributor

Jurigag commented Nov 24, 2017

OH i see it doesn't work with $this->useDynamicUpdate(true);

@Jurigag
Copy link
Contributor

Jurigag commented Nov 24, 2017

Well on phalcon 3.2.0 it works properly so i guess issue was fixed already.

@yesworld
Copy link
Author

@Jurigag
You didn't do the experiment quite correctly.
If the addition was done once, it's gonna work smooth.

If you create a new record, it will not work for you.
Try this:

class Users extends \Phalcon\Mvc\Model
{
    public $id;
    public $name;
    public $value=0;
    public function initialize(){
        $this->useDynamicUpdate(true);
    }
}

$Users = new Users();
$Users->value = 0;
$Users->save();

$id = $Users->id;
echo 'id = '. $id ;

var_dump(Users::findFirst( $id )->value);
$query = "UPDATE Users SET value = value + 1 WHERE id = :id:";
$di->get('modelsManager')->createQuery($query)->execute(['id' => $id ]);
$di->get('modelsManager')->createQuery($query)->execute(['id' => $id ]);
$di->get('modelsManager')->createQuery($query)->execute(['id' => $id ]);
var_dump(Users::findFirst( $id )->value);

echo phpversion("phalcon");

@yesworld
Copy link
Author

@Jurigag @sergeyklay
I tested it on the 3.2.4 version. The issue still exists.

@sergeyklay sergeyklay reopened this Nov 29, 2017
@sergeyklay sergeyklay self-assigned this Nov 29, 2017
@Jurigag
Copy link
Contributor

Jurigag commented Dec 4, 2017

On 3.3.x branch it works correctly. #13171 most likely this fixed it. Wait for phalcon 3.3.0 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants