Forums

WP MVC
Accessing other Models from a controller (8 posts)

  1. nhemsley
    Member
    Posted 1 year ago #

    I want to be able to access models from another controller from wp-mvc.

    Say I have a model Workshops, and Events, how would I access the Events object, (i.e. akin to $this->Workshop) from the WorkshopController?

    CHeers

    http://wordpress.org/extend/plugins/wp-mvc/

  2. nhemsley
    Member
    Posted 1 year ago #

    It appears that something like:

    $event = new Event();

    $allEvents = $event->find();

    will work, is this supported, or does the framework do some biolerplate setup?

  3. tombenner
    Member
    Plugin Author

    Posted 1 year ago #

    You'll want to use the load_model() or load_models() methods for this; here's how to use it:
    http://wpmvc.org/documentation/91/load_model/
    http://wpmvc.org/documentation/92/load_models/

    (Sorry, I had somehow overlooked including these in the documentation, but they're in there now.)

  4. nhemsley
    Member
    Posted 1 year ago #

    Thanks Tom,

    Looks good

  5. SummerGrand
    Member
    Posted 1 year ago #

    I'm trying to have one model's admin page show field values from an associated model. Just a column that converts the id to the correct name from the associated table. Using the events-calendar-example as a reference did not make it obvious how to do this "simple" task. I didn't find any direct means to reference the associated table's fields in the default_fields list. Instead, I had to create an access function referenced via value_method that just dereferences the object to pull out the correct name field from the associated table's object reference.

    It would be great if there were a direct way to refer to the associated table's fields in the default list. Maybe something like 'Venue.name' or 'venues.name'.

  6. julia_allyce
    Member
    Posted 10 months ago #

    @SummerGrand

    I'm looking for something similar... Could you share the fix you made?

  7. dodmax
    Member
    Posted 10 months ago #

    @SummerGrand
    value_method it is for me too!

    Apparently the function that displays admin table cells goes like this:

    public function admin_table_cell($controller, $object, $column) {
       if (!empty($column['value_method'])) {
          $value = $controller->{$column['value_method']}($object);
       } else {
          $value = $object->$column['key'];
       }
       return '<td>'.$value.'</td>';
    }

    wp-content\plugins\wp-mvc\core\helpers\mvc_helper.php line 188

    Doesn't seem to include any processing of this kind :(

  8. dodmax
    Member
    Posted 10 months ago #

    Here is a little patch that would allow to reference a sub object in the format 'Venue.name'

    wp-content\plugins\wp-mvc\core\helpers\mvc_helper.php line 188

    public function admin_table_cell($controller, $object, $column) {
       if (!empty($column['value_method'])) {
          $value = $controller->{$column['value_method']}($object);
       } else {
          $subs = explode('.', $column['key']);
          foreach ($subs as $sub)
             $object = $object->$sub;
          $value = $object;
       }
       return '<td>'.$value.'</td>';
    }

    You could also patch the header cell function line 171 for the name to display automatically:

    public function admin_header_cell($label) {
       return '<th scope="col" class="manage-column">'.MvcInflector::titleize(str_replace('.', ' ', $label)).'</th>';
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags