Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter nhemsley

    (@nhemsley)

    It appears that something like:

    $event = new Event();

    $allEvents = $event->find();

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

    Plugin Author tombenner

    (@tombenner)

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

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

    Thread Starter nhemsley

    (@nhemsley)

    Thanks Tom,

    Looks good

    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’.

    @summergrand

    I’m looking for something similar… Could you share the fix you made?

    @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 ??

    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>';
    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Plugin: WP MVC] Accessing other Models from a controller’ is closed to new replies.