Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
I can not change DB connection from one to the other inside module
  • Hello there,

    I have following config inside my module in config/development/db.php.

    return array(
      'default' => array(
        'connection'  => array(      
    'dsn'        => 'mysql:host=localhost;dbname=test',
          'username'   => 'root',
          'password'   => 'root',
        ),
      ),
      'test2' => array( 
    'type'        => 'pdo',
        'connection'  => array(
          'dsn'        => 'mysql:host=localhost;dbname=test2',
          'username'   => 'root',
          'password'   => 'root',
        ),
      ),
    );

    Then, I try to connect those database inside my rest API.

      public function post_index()
      {
        if(\Auth::check()) // It should connect to default
        {
          try      {
            return $this->response(Model_Test::getData()); // It should connect test2
          }catch(\Exception $e){
            \Log::error($e->getMessage());
          }
        }
        return $this->response(array('data'=>array()));
      }

    Also, I set $_connection = 'test2' in Model_Test and call something like this.

    $query = 'select * from tests';
    \DB::query($query,\DB::SELECT)->execute('test2'); 


    But, it dose not connect to test2.

    Do you know what is wrong with this?

    Best regards
  • Harro VertonHarro Verton
    Accepted Answer
    I can't reproduce that.

    See https://bin.fuelphp.com/snippet/view/O5 for my test code and result.
  • The reason why I could not connect to test2 database is that  the parameter was wrong like following.

    ・Wrong parameter
    \DB::list_columns($table_no,'test2');

    ・Correct parameter
    \DB::list_columns($table_no,null,'test2');

  • Thank you very much for your help :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion