Coder Social home page Coder Social logo

Comments (14)

swen100 avatar swen100 commented on May 31, 2024 1

Hi pengjun1128,

well, after nearly one year this bug is still there and nobody is interested in solving it further 😒

The thing I do is clearing the query-cache after every affected query:

\Phalcon\Mvc\Model\Query::clean(); 

from phalcon.

Jurigag avatar Jurigag commented on May 31, 2024

Well it's only problem with find/findfirst looks like:

$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 Robots extends \Phalcon\Mvc\Model
{
    public static $database = '';

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

    public function getSchema()
    {
        return static::$database;
    }
}

Robots::$database = 'phalcon_test';
var_dump(Robots::findFirst()->toArray());
/** @var Model\Manager $modelsManager */
$modelsManager = $di->get('modelsManager');
Robots::$database = 'phalcon_test2';
$robots = $modelsManager->load('Robots', true);
$query = new Model\Query('SELECT * FROM Robots', $di);
var_dump($query->execute()->toArray());
var_dump(Robots::findFirst()->toArray());
/vagrant/www/test.php:52:
array (size=7)
  'id' => int 1
  'name' => string 'Robotina' (length=8)
  'type' => string 'mechanical' (length=10)
  'year' => int 1972
  'datetime' => string '1972-01-01 00:00:00' (length=19)
  'deleted' => null
  'text' => string 'text' (length=4)
/vagrant/www/test.php:58:
array (size=0)
  empty
/vagrant/www/test.php:59:
array (size=7)
  'id' => int 1
  'name' => string 'Robotina' (length=8)
  'type' => string 'mechanical' (length=10)
  'year' => int 1972
  'datetime' => string '1972-01-01 00:00:00' (length=19)
  'deleted' => null
  'text' => string 'text' (length=4)

from phalcon.

Jurigag avatar Jurigag commented on May 31, 2024

Actually it's PHQL(once parsed phql queries to sql are no longer parsed again) caching problem, doing Model\Query::clean(); after query fixes an issue.

@sjinks @sergeyklay any idea how to fix it while still having phql caching?

from phalcon.

Jurigag avatar Jurigag commented on May 31, 2024

Well i guess https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/query.zep#L2440

Here uniqueId should takie into account database name? Like schema.

Just https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/query.zep#L2427 parse method require changes to fix this.

I just propose to move let model = manager->load(realModelName, true); from _prepareSelect to parse() method, set it as property and getSchema in parse and just use key like uniqueId!schema

Though i don't like this idea at all. Schema name should be IMHO parth of PHQL query so uniqueId is properly returned by internal parsing by selecting from diffrent database. If not provided will use default database.

from phalcon.

Jurigag avatar Jurigag commented on May 31, 2024

Though is parsing whole PHQL for different database even needed? Maybe we could somehow handle it in more "dirty" way for performance reasons?

from phalcon.

pengjun1128 avatar pengjun1128 commented on May 31, 2024

I also have this bug,how to fix it?

from phalcon.

swen100 avatar swen100 commented on May 31, 2024

this bug is sadly still present. Even with PHP 7.2.7 and Phalcon 3.4.0

Code

SQL

CREATE DATABASE `test_1`;
USE `test_1`;
CREATE TABLE `test_table` (
  `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `text` VARCHAR(255) NOT NULL DEFAULT 'blah',
PRIMARY KEY (id));
INSERT INTO `test_1`.`test_table` (`id`) VALUES (1); 

CREATE DATABASE `test_2`;
USE `test_2`;
CREATE TABLE `test_table` (
  `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `text` VARCHAR(255) NOT NULL DEFAULT 'blubb',
PRIMARY KEY (id));
INSERT INTO `test_2`.`test_table` (`id`) VALUES (1); 

PHP

<?php
class TestModel extends \Phalcon\Mvc\Model
{

    /**
     * @var string
     */
    public static $database = '';

    /**
     * @var string
     */
    public static $source = 'test_table';

    /**
     * @return string
     */
    public function getSource(): string
    {
        return static::$source;
    }
    
    /**
     * @return string
     */
    public function getSchema(): string
    {
        return static::$database;
    }

    /**
     * @return void
     */
    public function initialize()
    {
        $this->setConnectionService('db');
        $this->setSchema(static::$database);
        $this->setSource(static::$source);
        $this->keepSnapshots(true);
    }

}


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

        return $adapter;
    }
);


// I set the database/schema: 
TestModel::$database = 'test_1';

// I try to find an object which is in database 1
var_dump($obj_1->getSchema() );
var_dump($obj_1->toArray());
//Query is o.k, all works fine.

// I change the database/schema
TestModel::$database = 'test_2';

// I try to find an object which is in database 2
$obj_2 = TestModel::findFirstById(1);
var_dump($obj_2->getSchema() );
var_dump($obj_2->toArray());


# works
$modelsManager = $di->get('modelsManager');
$robots = $modelsManager->load('TestModel', true);
$query = new \Phalcon\Mvc\Model\Query('SELECT * FROM TestModel', $di);
var_dump($query->execute()->toArray());

Results

Note, that $obj_2->getSchema() gives the correct database!

string 'test_1' (length=6)

array (size=2)
  'id' => int 1
  'text' => string 'blah' (length=4)

string 'test_2' (length=6)

array (size=2)
  'id' => int 1
  'text' => string 'blah' (length=4)

array (size=1)
  0 => 
    array (size=2)
      'id' => int 1
      'text' => string 'blubb' (length=5)

from phalcon.

geoglis avatar geoglis commented on May 31, 2024

This bug is still present, (even with Phalcon 3.4.1 zephir version 0.10.10 ). Present since mai 2017!
To clean the query-cache after every affected query is no solution.
In my opinion, there were some good suggestions...
I know that not many users are affected with this bug, but thats no excuse for it that nobody seems to care about it.

from phalcon.

ruudboon avatar ruudboon commented on May 31, 2024

Added it this to 4.0. We need to investigate this. If this take a lot of effort we move it to 4.1.

from phalcon.

geoglis avatar geoglis commented on May 31, 2024

👍

from phalcon.

niden avatar niden commented on May 31, 2024

This actually cannot be fixed as it is described in the issue. The reason being is that the getSchema and getSource are final methods in the model for the v4 version.

The only two ways that I can think that one can achieve this would be by creating two models, where the source/schema are defined in each class's initialize method. The second option is to create a helper method that would use the Query Builder and would select the appropriate schema for your case.

In the future v4 versions we intend on introducing another ORM that will implement the data mapper pattern. At that point this task will become trivial.

from phalcon.

ruudboon avatar ruudboon commented on May 31, 2024

The impact for fixing this before 4.0 it too high. We need to move this to 4.1. If this is fixed early in the 4.1 development and we don't need interface changes we can cherry-pick this to merge into a 4.0.x release.

from phalcon.

stale avatar stale commented on May 31, 2024

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalcon.io/post/github-closing-old-issues

from phalcon.

swen100 avatar swen100 commented on May 31, 2024

This bug still exists. I just tried it with Phalcon v5.0.0beta3
:(

from phalcon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.