Anahkiasen / polyglot

Laravel localization and translation helper
130 stars 22 forks source link

Get model insert ID after save #6

Closed uldisrudzitis closed 10 years ago

uldisrudzitis commented 11 years ago

After saving (insert) new model everything is successfully inserted including translation, but I can't get the newly created model record ID.

... $record->save(); dd($record->id);

gives me null

When the model extends Eloquent the object is updated with ID after saving but Polyglot not.

Anahkiasen commented 10 years ago

I just tested on a fresh installation of Laravel and was able to fetch the ID as in your example. Can you show me your model and Lang model ?

jekjek commented 10 years ago

I can see the same behaviour with current laravel 4.1 branch and polyglot. Here is some demo code

The saving event method returns false and that cancels the save() method by eloquent. According to the documentation this is correct behaviour. I think that´s the reason why the inserted id is not accessible.

Nevertheless the data is saved correctly to the database due to a newly created model instance in the saving event on which save() is called before the language relation is processed.

If the saving event wouldn´t return false the record would be inserted twice. First time via the new model instance within the saving event method and another time when the actual model instance is saved by eloquent. Though, without a new model instance beeing saved, the relation record could not be inserted due to lack of foreign key.

tl;dr I am clueless how to fix it :(

jekjek commented 10 years ago

Fixed by latest commits:

https://github.com/Anahkiasen/polyglot/commit/ce48bc191650947c109d2f012f67d9fe925369ce https://github.com/Anahkiasen/polyglot/commit/b888140d6c4857cafb3efb7ef893aee145ed5b4d

Thanks @Anahkiasen and @jvdanilo!

Anahkiasen commented 10 years ago

Glad to si it's solved :)