• Hi,

    i’m reading into the WPMVC-plugin.

    i managed to generate some stuff for my plugin and now im trying to create forms.

    i have Towns and Countries, a Town belongs to a Country.
    So i want to place a dropdown for this, but i can’t figure out how.
    In the example-plugin there is something like this:

    <?php echo $this->form->belongs_to_dropdown('Country', $countries, array('style' => 'width: 200px;', 'empty' => true)); ?>

    i guess $countries has to be an array, but i can’t figure out where the array gets its values.
    can someone hint me to the point and method to populate $countries?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter MaWe4585

    (@mawe4585)

    I managed to do it,

    The controler needs methods for edit and add and there a set_countries method needs to be called. in this a list of countries is selected and written to tha $countries variable.

    class AdminTownsController extends MvcAdminController {
    
        var $default_columns = array('id', 'name');
    
        public function add() {
            $this->set_countries();
            $this->create_or_save();
        }
    
        public function edit() {
            $this->set_countries();
            $this->verify_id_param();
            $this->set_object();
            $this->create_or_save();
        }
    
        function set_countries() {
            $this->load_model('Country');
            $countries = $this->Country->find(array('selects' => array('id', 'name')));
            $this->set('countries', $countries);
        }
    }

    Now i have a new problem with this. I can save my town and assign a country to it. However, when i edit the town the country-dropdown is empty.
    in the database i can see it is saved, but the dropdown is still empty.
    i think i somehow have to add the selected country to the dropdown, but can’t figure out how.

    Thread Starter MaWe4585

    (@mawe4585)

    OK next problem solved, seems i hat the $belongs_to part in the model wrong.
    Now it looks like this:

    class Town extends MvcModel {
    
        var $display_field = 'name';
        var $includes = array('Country');
        var $belongs_to = array(
            'Country' => array(
                'local_key' => 'country', 'foreign_key' => 'id'
            ),
        );
    
    }

    My new problem is that when i edit the town, and change the value of the dropdown, it creates a new town instead of changing the Country of the current town.

    Thread Starter MaWe4585

    (@mawe4585)

    OK, the problem seems to be the $belongs_to part.

    if i do it like above, i have the problem described above.
    however if i do it like this:

    class Town extends MvcModel {
    
        var $display_field = 'name';
        var $includes = array('Country');
        var $belongs_to = array(
            'Country' => array(
                 'foreign_key' => 'country'
            ),
        );
    
    }

    as described here https://wpmvc.org/documentation/models/associations/
    i have the problem from my second post, that when viewing the edit-page, the dropdown is empty even if there is a value in the database

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wpmvc: how to populate belongs_to_dropdown?’ is closed to new replies.