Dropdown "setting default" value in edit mode in a Model not working?


#1

I have found in the forum this for setting default value for a dropdown but I do not know if this works in edit mode because in my case it does not work inside the Model…

here the code I have tried

´´´php
$this->hasOne(‘country_id’, [new Country(), ‘default’=>1]);

I have tried to use this line above to fix the problem in the semantic dropdown that does not work well with dynamic content taken from DB in my case these are Countries... 

In edit mode always it shows empty value even if in DB says the ID in that column... for country_id...

here my code in a model

```php

    function init() {
        parent::init();

        $this->hasOne('country_id', [new Country(),'required' => true, 'caption'=>'Country', 
            'ui'=>[
                'form'=>['AutoComplete']    
        ]])->addTitle();
//this is the new line added below
        $this->hasOne('country', [new Country(), 'default'=>1]);

I need that in edit mode the dropdown can show the saved option in the DB as selected and not showing empty…

so this line below:

        $this->hasOne('country_id', [new Country(), 'default'=>1]);

give me the following error:

Fatal Error
atk4\data\Exception: Reference with such name already exists


Exception Parameters

name: "#ref_country_id"
link: "country_id"
defaults: "defaults"

if I change country_id for country like below to avoid the error above like so:

        $this->hasOne('country', [new Country(), 'default'=>1]);

it gives me empty anyway in edit mode for that dropdown, so it does not refresh the saved value from DB in the form :(…

In semantic I know you have to use the line below:

$('.dropdown').dropdown('set selected', '1');

but in ATK4 how can I set this if the line above does not work?

Also how can I get in ATK4 the value of that field when in edit mode so I can pass it to the dropdown in edit mode or it is automatic? then how to do it and make it work?

any ideas to fix this with dropdowns that are dynamically taken the data from the DB?

How in ATK4 detect the edit mode in a Model?

Thanks for any tips!