Closed keech closed 9 years ago
Sorry, I haven't considered scattering DB. So this issue should not be solved. I should set DB id as argument if I use transaction.
Yes.
But there is no method to give connection to Orm\Model. It is still a problem.
It should be easy enough to add, I'll reopen this and take a look at it later/tomorrow.
Thanks.
It may be better Orm\Query
also has a method to set connection like:
$result = Model_User::find()
->where('user_id', $user_id)
->where('status', Model_User::STATUS_ACTIVE)
->connection('slave')
->get_one();
above code is from http://php6.tumblr.com/post/38309122629/fuelphp-advent-calendar-2012
@Keech As @kenjis says you can set the connection using the ->conection()
method from the Model class. Due to this I don't see a need to add a similar method to Query as you do not create instances of Query manually. You should use Model::query()
which will pass the needed instance to the Query object for you.
On closer inspection it seems there was no method to set the active DB instance to use so I have added this in the above commit.
@stevewest I wanted to say, both Orm\Model
and Orm\Query
should have methods to set connections. Orm\Query
has no method to set connections?
If Orm\Query
had them, we could write like this:
$result = Model_User::query()
->where('user_id', $user_id)
->where('status', Model_User::STATUS_ACTIVE)
->connection('slave')
->get_one();
You added the methods to Orm\Model
, so now we can set them via Orm\Model
.
Model_User::set_connection('slave');
$result = Model_User::query()
->where('user_id', $user_id)
->where('status', Model_User::STATUS_ACTIVE)
->get_one();
Don't you think we need to add the methods in Orm\Query
, do you?
I don't see any issue with adding that. I can do that now.
The first example you show there should now work with the above commit.
Problem
DB instance in transaction are different from the db instance made when the transaction starts.
Details
When I create database connection like below,
$db is instance of below.
But,
will be used
now.
My thinking
We should be able to use the same DB instance both DB instances in transaction and DB instance made when the transaction starts if argument of DB name is not provided.