Use of $foreign_key in advanced modules

edited July 2011 in News & Announcements
What's the best practice when it comes to implementing related models within advanced modules?

The tutorial in the user guide covers simple modules :

But when i look at the Blog module code, the $foreign_key field is never used. Instead, there's a number of custom functions that achieve the same result.

The thing is, the code as shown in the tutorial doesn't really apply to advanced modules (as stated here - even though i'm still unable to make this work for some reason...) and I'm not sure if I should keep on trying to understand why is it that the foreign_key field doesn't seem to work for me, or just do it like the blog module does...

Can you please shed some light?


  • edited February 2012
    If your foreign key model exists in another module, then instead of a string value for the name of the model, you use an associative array with the key being the name of the module(as shown in that comment).
    public $foreign_keys = array('id_ads_categories' => array('ads' => 'ads_categories_model');

    The foreign_keys model attribute was added after the blog was developed and some of that may just be because we never went back and changed it.

    Adding foreign keys should do a couple things for you:
    # Create a dropdown of the values you need to select from instead of a text field in the form_fields model method (see around line 1750 of MY_Model)
    # Will auto load other models on record objects (see around line 2985 of MY_Model)

    What is happening?
  • I think there's a type in admin's response. The key to the associative array should be the name of the *module* and the value should be the name of the *model*.
  • edited 3:48PM
    Thanks... I've fixed the text to avoid confusion.
Sign In or Register to comment.