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

Events didn't fire for sharding db?

I have two logger, one for app, the other for db profiler the app.log works fine. but the db.log records nothing when requests end. The difference is kind of : foreach( $dbConfig, $schema => $config) { $di->set('db_'.$schema, function() use ($di, $schema, $config)) { $conn = new xxxxx; $evtMgr = $di->getShared('eventsManager'); $evtMgr->attach('db'.$schema, new dbListener()); $conn->setEventsManager($evtMgr); return $conn; } }

In class dbListener there is a function afterQuery($event, $conn) writing the log for db statement. The problem is, the model works fine with splited table and db, but it seems the listener's callback has never been called. Is there anyone meets the same problem here?

More interesting... In class Test extends Model: $test = Test::Find( xxx ); $conn = $this->getDI()->get('dbtest'); $conn->query("xxx"); $conn->getEventsManager()->fire('dbtest:afterQuery', $conn);

the ::Find() method will log two lines of db select, the query() will log once. I wonder how ::Find() works.. it seems it cause twice calling of getReadConnection(), which I override as follow: public function getReadConnection() { return $this->getDI()->get('db' . $this->getSchemaName()); }



404

If i understand you right (my English is poor), you asking about why is Model::find is doing 2 select queries, but db->query only one for similar sql queries?

It because Model is needed to select table schema first to get info about related table (fields, their types, etc.). And if you make a mistake, then Model will generate an error before send the query to SQL server. But db->query is a raw SQL and all mistakes would be sended to SQL server.

Actually there are two problems..

  1. The dbListener for DbAdapter is not working until I called fire();
  2. Model::find() seems sending "select" statement twice. Just as you mentioned, it sends two queries to the sql server, I've checked the mysql log. Thanks~
    But I wonder if it will cause some effect lost?


98.9k

@LongLong can you try registering service 'db' as shared?

$di->setShared('db', function() { 
  //... 
});

@phalcon I've tried shared service 'db', the event callback still can't be called, otherwise fire it manually after that. Is there some way to debug with this, with gdb?



98.9k

There is no need for gdb, you can use xdebug as any other php application