Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

translate on different connection, uses default connection #283

Closed
reno1979 opened this issue Oct 18, 2016 · 9 comments
Closed

translate on different connection, uses default connection #283

reno1979 opened this issue Oct 18, 2016 · 9 comments

Comments

@reno1979
Copy link

reno1979 commented Oct 18, 2016

When I have two connection possibilties (mysql1, mysql2) and I try the following:

$featureGroup = \App\FeatureGroup::on('mysql2')->firstOrCreate(['code'=>"general"]);
$featureGroup->translate('en')->name = 'general';
$featureGroup->translate('nl')->name = 'algemeen';
$featureGroup->save();

This saves the new row with code value general into the mysql2 database, but the translations are never stored. (even when I also use the setConnection('mysql2') on each translate and save method)

\App\FeatureGroup::on('mysql2')->getModel()->create(['code'=>"general", 'en'=>['name'=>"general"],'nl'=>['name'=>"algemeen"]]);

also fails

update

When I dump the created model instance (using second connection) , it appears the realtion to the translation table has not the same value in its connection parameter.

It seems related to Elequent relations not using its parent connection, see this issue::

laravel/framework#9355

@reno1979
Copy link
Author

reno1979 commented Jan 13, 2017

@dimsav

see (laravel/framework#16103)

After using the laravel parent connection fix we still have problems inserting translations on an other connection.

We currently use this to be able to continue, but it is a bit ugly....

// create a new feature group entry
$featureGroup = new App\FeatureGroup;
$featureGroup->setConnection('otherconnection'); // Don't forget to set the connection!
$featureGroup->save();
 
// Now we will create the translation.
$featureGroupTranslation = new App\FeatureGroupTranslation;
$featureGroupTranslation->setConnection('otherconnection'); // Don't forget to set the connection here either.
 
$featureGroupTranslation->feature_group_id  = $featureGroup->id; // Grab the id of the newly created featureGroup.
$featureGroupTranslation->name              = 'algemeen';
$featureGroupTranslation->locale            = 'nl';
 
$featureGroupTranslation->save(); // And save the translation.

@dimsav dimsav reopened this Jan 16, 2017
@dimsav
Copy link
Owner

dimsav commented Jan 27, 2017

@reno1979 can you please make a pull request ?

@reno1979
Copy link
Author

reno1979 commented Jan 27, 2017

@dimsav

Inserting a pull does not seem to work.

I also do not have the solution / fix for your code, so I can't suggest a fix.

@reno1979
Copy link
Author

reno1979 commented Feb 3, 2017

@dimsav

I would like to add a pull request, but did not figure out how.
What do you need?

@dimsav
Copy link
Owner

dimsav commented Feb 3, 2017

I think we need to setup the tests with a second db connection and make the tests fail asserting the expected behavior and the code above.

If you need help about how to make a pull request try googling it.

:-)

@reno1979
Copy link
Author

reno1979 commented Feb 3, 2017

@dimsav The tests are a good start.

I will not be making a pull request, because then I suppose to have a source code change proposal. And that is the part that I have not figured out yet, I don't think google did either :P

I hoped this plugin would benefit from the fixes in Laravel 5.4 regarding relations on other connections.

@dimsav
Copy link
Owner

dimsav commented Feb 3, 2017

Leaving this open in case somebody from the community wants to contribute.

@vocanic-saumini
Copy link

vocanic-saumini commented Jun 12, 2017

@dimsav

Derived from @reno1979 solution. Thinking we can add following line $translation->setConnection($this->getConnectionName()); in saveTranslations function after the if condition to fix the issue

@Gummibeer
Copy link
Collaborator

it's fixed

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants