voskobovich/yii2-many-to-many-behavior

Ono to one relations

Closed this issue · 3 comments

Hi! Thanks for your script!
Can you please add one to one relations?

I changed your code a bit

$bindingKeys = $newValue;

if(!is_array($bindingKeys))
{
$bindingKeys = ['0' => $bindingKeys];

}

After I can use one to one relations, but always when you update your script and I do composer update I losing it. Maybe you can just add it into your code, please?

Of course!
But I need a real life example to understand the situation and what kind of data you are working.

I ask you to make a pull request, and send it for review.

Thanks for your quick answer!

                <?=$form->field($user, "hobbies")->widget(Select2::classname(),[
                    'hideSearch' => true,
                    'value' => '', // initial value
                    'data'=>ArrayHelper::map(Hobbies::find()->all(), 'id', 'name_ru'),
                    'showToggleAll' => false,
                    'options' => [
                                    'placeholder' => 'Select...', 
                                    'multiple' => true,
                                    //'class' => 'form-group',
                                ],
                    'pluginOptions' => [
                        'allowClear' => true,
                    ],
                ])->label('Hobby'); ?>

If I change 'multiple' => true, to 'multiple' => false I need to select only one value from the list your script shows me error Invalid argument supplied for foreach()
foreach ($bindingKeys as $relatedPk) {

That is why I added this code

$bindingKeys = $newValue;

if(!is_array($bindingKeys))
{
$bindingKeys = ['0' => $bindingKeys];

And after I can use it without any error messages

The behavior is expecting either an array of ids or no attribute value at all. All other kinds of input need to be transformed into this internal representation using custom getters and setters.

There is no need to update the behavior itself, implement your logic in getters/setters.