-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.4] Use parent connection if related model doesn't specify one #16103
Conversation
Is this going to be pushed back into 5.1 LTS as well? |
@davisonja not sure, 5.1 is technically closed for adding features, only bug fixes :) |
Just for a reminder, what is the current behavior? It just uses the default connection |
Yes it uses the default connection if not Model $connection was set. With this PR there's no way to fallback to default no, unless you set the Model $connection to use the default connection. Not |
The reason behind this change is multi-tenancy, to be able to do:
This will bring the artists of tenant |
@themsaid True, I still feel it's a bug, though I concede that's primarily as I can't think of a use case for wanting
to (always) return artists from the default connection; and I am currently using it in a situation where we need the artists to come from 'band-123'. |
@themsaid @taylorotwell I would really appreciate this fix. We 'clone' parts of our database models into multiple connections, and because they are the same we can use our Eloquent Models on each connection. When working with an eloquent model instance on a specific connection, I did not expect that the related models of that instance would be using the applications default connection. Causing corrupted data in the database or errors. We 'solved' it for now using a queue on a new thread, that changes the default database connection before the process starts. This is not a real solution and feels like a hack. We would like to be able to retrieve :
One note: It would also fix this issue on the translate extension: |
@themsaid @taylorotwell what's the status on this one? |
why only 5.4 and not the current improvements on 5.3? when will 5.4 come out? |
here: https://laravel-news.com/2016/01/laravel-release-process/ i see it'll be december? |
Might not be then. Probably more like January, though it's not been decided. |
@GrahamCampbell why not in the sub updates in 5.3? |
This is a breaking change so can't go to 5.3. |
@GrahamCampbell aah, when changing composer to 5.4 we can allready use the development version of 5.4 right? because this is an important one to us... |
We inserted the code from the patch (Eloquent/Model.php) , but can't seem to get it working.
|
@reno1979 but the example you shared doesn't involve related models, which is the purpose of this PR. Can you please open a new issue with an example on how to generate the issue? But without using a package, just a fresh laravel app cause packages may have changed the default behaviour. |
Thank you for the quick reply. We are not using any packages at the moment, but the create method keeps using the default db. We are using artisan tinker, and tried the following:
|
Here's a helpful hint to setup a base model with this change now in place for 5.4. If some of your models use an alternate connection, then you can override the default connection while still keeping it for all other models by having them all inherit from this class. <?php
namespace App;
use Illuminate\Database\Eloquent\Model as BaseModel;
class Model extends BaseModel
{
/**
* Get the current connection name for the model.
*
* @return string
*/
public function getConnectionName()
{
return config('database.default');
}
} |
In reference to this #9355 (comment)
Currently there's no way to query posts on the parent connection "non_default_connection":
This PR will use the parent model connection if the related model doesn't have a connection specified.