Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
count() with multiple db-connections
  • Hi all,
    I think, i found a bug. 

    $ticket = Model_Ticket::query()
            ->count()
             ->get();

    Fuel\Core\Database_Exception [ Error ]: SQLSTATE[42S02]: Base table or
    view not found: 1109 Unknown table 'default_db_prefix_t0' in field list
    with query: "SELECT COUNT(DISTINCT `default_db_prefix_t0`.`id`) AS
    count_result FROM `ts_tickets` AS `ts_t0`"

    -------------------------------------
    I have a view connections:
    ..app\config\db.php:
    'default' => array( ...'table_prefix' => 'default_db_prefix_',..)
    'blub' => array( ...'table_prefix' => 'ts_',..)
    --
    And my model did use the default:

    ..\fuel\app\classes\model\ticket.php:
    class Model_Ticket extends Orm\Model{
        protected static $_connection = 'lyts';

    You see, fuel try to use  - why ever - both connections.
  • Which Fuel version are you using? I can not determine this from your information.

    I see you define the connection name 'lyts' in your model, which does not exist in your db.php, so this can never work (the DB driver throws an exception if an unknown name is passed).
  • @Verton
    Thank you for the fast response.
    Of course I set the db-connection in "...\fuel\app\config\db.php". And all other is workings with the connection and model is very well.
    While starting my project ~1.5month ago, I downloaded the last version. So I don't know if it is stell the last.
    In changelog.md and readme.md the version is titles 1.5 (so not the last version). But the copyright-date in the license-file is "2010 - 2011". So I can't be sure.
    I just started with webdevelopment and FuelPHP.

  • HarroHarro
    Accepted Answer
    Ok,

    So, to recap:
    - you have multiple connections defined in your db.php
    - your model has a non-default connection defined
    - the query still uses the default connection

    This is only on a count() query, or on all queries run on the model?

  • Hi,
    I figured out, Fuel is always using the default connection for the count-method till when there was no "get()" before. I solved it with an other way.
  • I'm a bit confused now.

    Looking back at your original post, it suggests you were talking about an ORM model. But that doesn't use a get() at all, and the count() method does use the defined connection (the read-connection in case you defined both a read and a write connection) in the model. All ORM query methods do.

    So $ticket = Model_Ticket::query()->count()->get();

    could have never worked, as the count() method returns an integer you can't chain on?


Howdy, Stranger!

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

In this Discussion