OmgDef / yii2-multilingual-behavior

Yii2 port of the yii-multilingual-behavior.
146 stars 60 forks source link

Suggestion #12

Closed andreCatita closed 9 years ago

andreCatita commented 9 years ago

I found something new you could add to your ml behavior as a flag It's an enhancement if you wish to your 2.0 milestone, I added it to my project.

By default, the behavior saves all languages, even if all translated fields are empty. Since I don't want that, to avoid having meaningless rows in my case, I just added a quick verification:

in saveTranslations, before running through the attributes I declared:

$noEmptyFields = false;

Inside the for, I have:

if (!empty($value))
     $noEmptyFields = true;

And when it comes to save, I just check the flag:

if ($noEmptyFields)
     $translation->save(false);
OmgDef commented 9 years ago

@andreCatita Good idea, but if you want to delete all previously set values for the translation, the translation model will not be saved

echo $model->title_fr; // title fr
echo $model->body_fr; // body fr

$model->title = 'test title';
$model->body = 'test body';
$model->title_fr = '';
$model->body_fr = '';
$model->save();

$model = Post::findOne($model->id);
echo $model->title_fr; // title fr
echo $model->body_fr; // body fr
andreCatita commented 9 years ago

@OmgDef True.. You see any workaround?

I don't even think we can tell if we are updating or creating it the first time, seeing as it's Yii handling it.

OmgDef commented 9 years ago

@andreCatita I implemented this feature

andreCatita commented 9 years ago

@OmgDef Yeah, I had just managed to fix it myself, I however was still verifying with the (!empty()), coz' I didn't look into your getLangAttribute, but if you added your flag into the != null verification, it's because you've taken it into consideration.

Good job.