Support » Plugin: WP MVC » [Plugin: WP MVC] accessing associated models in view

  • I’ve a model with a ‘belongs_to’ association with another model. I’m hoping that in the view I can dereference through to an instance of the associated model to get properties of it?

    class JungleMonkey extends MvcModel {
    	var $display_field = 'name';
    	var $belongs_to = array(
    		'JungleBranch' => array(
    			'foreign_key' => 'favourite_branch'

    in my view I’d like to do something like:

    <?php echo $object->favourite_branch->name; ?>

    I’m guessing I can do this myself in the controller, but since all the meta data is in place, I wondered if the framework already handles this (or whether it’s planned to in the future).

    – thanks for the great work btw 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi,

    Try adding var $includes = array('JungleBranch'); in your model.
    This way it should query it for you.
    Also try to print_r($object) somewhere to make sure it is / is not inside.

    Actually it should query the value automagically for you since $object has a __get() overload that does it for you.
    Just access it like this: $object->jungle_branch->name;

    Thanks for the reply and I can confirm that both of these approaches work perfectly.

    I must admit it leaves me wondering how I’m going to handle the situation in the future when I want two different fields in the same model to reference two separate instances of a second model, given that I’m referencing the associated model by it’s name.

    An example in the original context might be

    var $belongs_to = array(
    	'JungleBranch' => array(
    		'foreign_key' => 'favourite_branch'
    	'JungleBranch' => array(
    		'foreign_key' => 'home_branch'

    I’m thinking of something in terms of

    $includes = array(
    	'favourite_branch' => 'JungleBranch',
    	'home_branch' => 'JungleBranch'

    Which maybe would change both the $object->favourite_branch and $object->home_branch from being just ids, into to being the appropriate instances of the JungleBranch model.

    Is there a way to do this at the moment, or anything in the works to enable it? If not I might have a shot as doing a patch for it.

    Had this problem too, have a look here:

    It doesn’t work with $includes though, I’m having an error on my side (looks like a bug, didn’t took time to dive in yet).

    Also in the future if you use has_and_belongs_to_many associations and try to get the value via __get() you might wanna apply this patch since there is a bug in v1.2

    Thanks for all your help with this dodmax, it really helped.

    I ended up hitting another issue where I wanted to join with another table, but not based on the primary key! I’m working on a patch for that on my branch where you can specify a ‘local_key’ as follows…

    class Person extends MvcModel {
    	var $has_many = array('NameMeaning' => array('local_key' => 'fname', 'foreign_key' => 'name'));

    I’ll check it in once I’ve got some confidence in it. But that set me thinking about testing – might be good to build some tests around wp-mvc. There’s serveral of us hacking on it now.

    DODMax, do you have any preferences for a testing framework? I’ve used
    Mockery before and got on well with it. I might start putting some tests together based on that if no-one else has done anything on testing yet.

    Hi davelopware,

    Glad that I could help! No extensive testing on my side yet, have to work really quickly for now 🙂

    I like WP-MVC, it lacks some features but it’s an amazing piece of work and it works well.
    It’s true that there is some interesting hacks on Github. Hope we’ll be able to put together a comprehensive pull request and that the author will take some time to examine it!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Plugin: WP MVC] accessing associated models in view’ is closed to new replies.