Propel2 icon indicating copy to clipboard operation
Propel2 copied to clipboard

propel sql:insert ignore ['settings']['queries'] database config parameter

Open Uvadzucumi opened this issue 10 years ago • 4 comments
trafficstars

Example: I have "set names" query in my mysql connection config, and add initial sql data queries file in generated sqldb.map file. additional sql file contained data in utf-8 codepage and incorrectly added after run propel sql:inisert command. For fix this problem I add dirty hack in src/Propel/Generator/Manager/SqlManager.php file. method insertSql() after line 170 part of generated config:

//part of generated config file
$manager->setConfiguration(array (
  'classname' => 'Propel\\Runtime\\Connection\\ConnectionWrapper',
  'dsn' => 'mysql:host=localhost;dbname=mydb',
  'user' => 'xxx',
  'password' => 'xxx',
  'settings' =>
  array (
    'charset' => 'utf8',
    'queries' =>
    array (
      'utf8' => 'SET NAMES utf8 COLLATE utf8_unicode_ci, COLLATION_CONNECTION = utf8_unicode_ci, COLLATION_DATABASE = utf8_unicode_ci, COLLATION_SERVER = utf8_unicode_ci',
    ),
  ),
));
// ...

fix

// dirty hack in insertSql method
// ...
// next line #168
            $con = $this->getConnectionInstance($database);

            if(isset($this->connections[$database]['settings']['queries']) && count($this->connections[$database]['settings']['queries'])){
                $init_queries=$this->connections[$database]['settings']['queries'];
            }else{
                $init_queries=array();
            }

            $con->transaction(function () use ($con, $sqls, $init_queries) {
                foreach ($init_queries as $sql){
                    try {
                        $stmt = $con->prepare($sql);
                        $stmt->execute();
                    } catch (\Exception $e) {
                        $message = sprintf('SQL insert failed: %s', $sql);
                        throw new \Exception($message, 0, $e);
                    }
                }

                foreach ($sqls as $sql) {
                    try {
                        $stmt = $con->prepare($sql);
                        $stmt->execute();
                    } catch (\Exception $e) {
                        $message = sprintf('SQL insert failed: %s', $sql);
                        throw new \Exception($message, 0, $e);
                    }
                }
            });

// ...

Uvadzucumi avatar Jul 01 '15 15:07 Uvadzucumi

I see, that should actually happen in the construction of the PDO instance.

marcj avatar Aug 07 '15 15:08 marcj

This is still valid. Is there any workaround or do I have to use this hack? I am using 2.0@dev

djavorek avatar Aug 03 '20 13:08 djavorek

@djavorek Do you have a fix you can PR to this repo? We are happy to review and merge in any contributions here.

dereuromark avatar Aug 03 '20 13:08 dereuromark

Will do it later this day.

djavorek avatar Aug 03 '20 13:08 djavorek