Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Unable to connect to default database
  • Hello there.

    I am not able to connect default database.

    For example, I have database A and B.
    Database A is default database and the both A.user and B.user have similar schema.
    .
    1. I connect to A.user.
    2. I connect to B.user.
    3. Connecting back to A.user.
    4. Then, somehow what I get is B.user data.
    Do you know why this is happening?

    Possibly, program is holding the database B instance or something.

    So, I tried following but it dose not work at all.

    unset(static::$_cached_objects[get_called_class()]);

    Here is my  part of source code from numbering 3.

    Model_User::clear_cache(); 
    Model_User::set_connection('default'); 
    Model_User::set_original_properties(); // Sets original properties 
    $user = Model_User::find_by_username('aaaBBBBB');

    The error message says "Column not found" because of referencing wrong database.

    I set to default database but it try to connect to wrong database.

    I also check following and it says "default".

    Model_User::$_connection

    Best regards,
  • It seems it connects to right database but using wrong $_properties.

    Do you know how to reset the orm $_properties?

    I tried unset(static::$_properties['some_attribute']), but not working at all.
  • An ORM model is record representation, so is it always linked to a single table of a single database.

    It will probably be possible, with quite a bit of effort, to cleanly swap databases, but it will never work if the table's schema in the database isn't completely identical.
  • okay.
    But, it is possible to change $_properties.
    Why is that?
    Or, it is not intended to change?
  • From within the class, everything can be changed. That doesn't mean you should.

    Everything that is defined as "static" is global, for all instances of the class. So changing it will have an impact on all instances, also the onces already created with the old property list.
  • Yes, that is what I expect.
    Well...I might have to find another way.
    Thank you.
  • You could by explicitly closing the database connection before you change databases and try to access the new database:

    static::$_connection and static::\DB::instance(static::$_connection)->disconnect();
    static::$_write_connection and static::\DB::instance(static::$_write_connection)->disconnect();

    Then set a new connection on the model, make your changes, and try again.

    I assume that because the connection is active, it will keep using that connection. The database layer does not do an explicit "USE" when you connect, so if both database are served by the same database engine, changes are the same connection is re-used.
  • I see.

    Thank you for that.

    I will try that.


  • Hello,

    I solved myself.

    To refresh the $_properties, I did following.

    static::$_properties_cached = array();
    static::$_properties = array( 'id','username','nickname','email');

    Do you think this is okay?

  • HarroHarro
    Accepted Answer
    You shouldn't do that, that erases the entire ORM properties cache.

    Instead, use

    $class = get_class();
    static::$_properties_cached[$class] = array();

    to only erase the cache of the current model class.
  • I did that.

    But, it gives me following error.

    Fuel\Core\FuelException [ Error ]:
    No properties found in model.

  • HarroHarro
    Accepted Answer
    Sorry, my bad, the code uses array_key_exists.

    So it should be

    unset(static::$_properties_cached[$class]);
  • Thank you for your help.
    Now it is working.

Howdy, Stranger!

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

In this Discussion