Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Transaction models with other connection failed

Hi guys, I have a project with 2 connections (mysql, and postgresql), postgresql is default connection in DI as $di->set("db", func....) and Mysql is $di->set("db-cms", func...), When I try to do a transaction with phalcon transaction manager in mysql models its not working I get an error because it takes the postgresql connection ""i guess"" because not recognize table from transaction.

Look error:

#0 [internal function]: PDOStatement->execute() 
#1 [internal function]: Phalcon\Db\Adapter\Pdo->executePrepared(Object(PDOStatement), Array, Array) 
#2 [internal function]: Phalcon\Db\Adapter\Pdo->execute('INSERT INTO "me...', Array, Array) 
#3 [internal function]: Phalcon\Db\Adapter->insert('menu', Array, Array, Array) #4 [internal function]: Phalcon\Mvc\Model->_doLowInsert(Object(Phalcon\Mvc\Model\MetaData\Memory), Object(Phalcon\Db\Adapter\Pdo\Postgresql), 'menu', 'id_menu') 
#5 /Users/julianmolina/Documents/htdocs/base/app/admin/controllers/MenuController.php(68): Phalcon\Mvc\Model->save() 
#6 [internal function]: Logisticapp\Admin\Controllers\MenuController->newAction() 
#7 [internal function]: Phalcon\Dispatcher->dispatch() #8 /Users/julianmolina/Documents/htdocs/base/public/index.php(30): Phalcon\Mvc\Application->handle() 
#9 {main}
//Bootstrap.file

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db', function () use ($config) {

    $config = array(
        "schema" => $config->database->schema,
        "host" => $config->database->host,
        "dbname" => $config->database->dbname,
        "username" => $config->database->username,
        "password" => $config->database->password,
    );

    return new \Phalcon\Db\Adapter\Pdo\Postgresql($config);

});

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db-cms', function () use ($config) {

    $config = array(
        "host" => $config->database_cms->host,
        "dbname" => $config->database_cms->dbname,
        "username" => $config->database_cms->username,
        "password" => $config->database_cms->password,
        "options" => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        )
    );

    return new \Phalcon\Db\Adapter\Pdo\Mysql($config);

});
//controller

   /**
     *
     */
    public function newAction()
    {

        $form = new MenuForm(null, array("edit" => false));

        if ($this->request->isPost()) {

            try {

                $request = $this->request;
                $manager = new TransactionManager();
                $transaction = $manager->get();

                $menu = new Menu();
                $menu->setTransaction($transaction);
                $menu->id_menu = null;
                $menu->id_type_menu = $request->getPost("id_type_menu", "int");
                $menu->id_language = $request->getPost("id_language", "int");
                $menu->menu_name = $request->getPost("menu_name", "striptags");
                $menu->paragraph = $request->getPost("paragraph", "striptags");
                $menu->address = $request->getPost("address", "striptags");
                $menu->logo = $request->getPost("logo", "string");
                $menu->status = 1;

                if ($menu->save() == false) {
                    $this->_printMessages($menu->getMessages());
                    $transaction->rollback();
                } else {
                    $roleHasMenu = new RoleHasMenu();
                    $roleHasMenu->setTransaction($transaction);
                    $roleHasMenu->id_menu = $menu->id_menu;
                    $roleHasMenu->id_role = $this->session->get("auth")->id_role;
                    if ($roleHasMenu->save() == false) {

                    } else {
                        $transaction->commit();
                        $this->flash->success("Registro completado.");
                    }
                }
            } catch(\Phalcon\Mvc\Model\Transaction\Failed $e){
                $this->flash->error('Failed, reason: '. $e->getMessage());
            } catch(\PDOException $e){
                $this->flash->error($e->getTraceAsString());
            }
        }

        $this->view->form = $form;
    }
//Model

/**
 *
 */
class Menu extends \Phalcon\Mvc\Model
{

    /**
     * Primary Key
     * @var Integer
     */
    public $id_menu;

    /**
     * @var Integer
     */
    public $id_language;

    /**
     *
     */
    public $id_type_menu;

    /**
     * @var String
     */
    public $menu_name;

    /**
     * @var String
     */
    public $address;

    /**
     * @var String
     */
    public $logo;

    /**
     * @var Integer
     */
    public $status;

    /**
     *
     */
    public function initialize()
    {
        $this->setConnectionService("db-cms");
        $this->setReadConnectionService("db-cms");
        $this->setWriteConnectionService("db-cms");
    }

}


83.4k

Can you please the error message you're getting, only the backtrace was posted

Can you please the error message you're getting, only the backtrace was posted Hi, this error:

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "menu" does not exist LINE 1: INSERT INTO "menu" ("idtypemenu", "idlanguage", "menunam... ^



83.4k

Which version of Phalcon are you using?

edited Apr '15

1.3.4 and id =>1030441

Which version of Phalcon are you using?



33.6k
Accepted
answer

Try returning the connection directly:

class Menu extends \Phalcon\Mvc\Model
{
   public function getWriteConnection()
   {
       return \Phalcon\DI::getDefault()->get('db_cms');
   }
   public function getReadConnection()
   {
       return \Phalcon\DI::getDefault()->get('db_cms');
   }