Skip to content
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.5] Add factory model creation on different connections #20191

Merged
merged 2 commits into from
Jul 23, 2017
Merged

[5.5] Add factory model creation on different connections #20191

merged 2 commits into from
Jul 23, 2017

Conversation

casperboone
Copy link
Contributor

@casperboone casperboone commented Jul 20, 2017

This PR allows for the creation of 'factory models' on other connections than the default.

Example usage:

factory(User::class)->connection('ext-02')->create();

Currently, for the same behavior, you would have to make() the model, then use setConnection() and manually save() it.

@ntzm
Copy link
Contributor

ntzm commented Jul 20, 2017

Isn't that what the $connection property on the models is for? What's the use case for this?

@themsaid
Copy link
Member

@ntzm so you can create a factory model on a different connection.

@taylorotwell
Copy link
Member

@casperboone it seems there could be some unexpected behavior if you do:

factory()->connection()->make()...

The model will not have the connection you specified set. That might be unexpected since it works for create.

@casperboone
Copy link
Contributor Author

casperboone commented Jul 20, 2017

@taylorotwell Good catch! Will add that :)

@casperboone
Copy link
Contributor Author

casperboone commented Jul 20, 2017

Done 😃 .

The changes involved moving

$instance->setConnection($instance->newQueryWithoutScopes()->getConnection()->getName());

to the makeInstance() method. I believe this will not change any behavior (and tested this quickly), but please correct me if I'm wrong.

@tillkruss tillkruss changed the title Add factory model creation on different connections [5.5] Add factory model creation on different connections Jul 20, 2017
@themsaid
Copy link
Member

@casperboone the side effect to your change is that now factory()->make() will generate models that have the connection property set, before your change the resulted model will have the connection property = null, since it's not persisted yet I believe the connection should remain null.

@taylorotwell
Copy link
Member

Yeah I think you can keep current behavior if connection is not explicitly set.

@casperboone
Copy link
Contributor Author

@themsaid Good point! Changed the code. Didn't like having to add two conditionals for this, but seems like the simplest way to keep the current behavior (and support the new 😄).

@taylorotwell taylorotwell merged commit c67c5a6 into laravel:master Jul 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants