Jump to:

5451 Posts in 1673 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Model Admin Automatic Linkage

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 248 Views
  • kaanuni
    Avatar
    Community Member
    22 Posts

    Model Admin Automatic Linkage Link to this post

    Hello,

    I have created a bunch of geographic Dataobjects. Here is the code:

    <?php
    class Region extends Dataobject {

       public static $db = array(
        'Name' => 'Varchar(30)'
       );

       public static $has_one = array(
       );
       
       public static $has_many = array (
        'Countries' => 'Country',
        'States' => 'State',
        'Provinces' => 'Province',
        'Cities' => 'City',
        'Districts' => 'District',
        'Hotels' => 'Hotel'
       );

    }

    <?php
    class Country extends Dataobject {

       public static $db = array(
        'Name' => 'Varchar(30)'
       );

       public static $has_one = array(
        'Region' => 'Region'
       );
       
       public static $has_many = array (
        'States' => 'State',
        'Provinces' => 'Province',
        'Cities' => 'City',
        'Districts' => 'District',
        'Hotels' => 'Hotel'
       );

    }

    .
    .
    .

    <?php
    class District extends Dataobject {

       public static $db = array(
        'Name' => 'Varchar(30)'
       );

       public static $has_one = array(
        'City' => 'City',
        'Province' => 'Province',
        'State' => 'State',
        'Country' => 'Country',
        'Region' => 'Region'
       );
       
       public static $has_many = array (
        'Hotels' => 'Hotel'
       );

    }

    I have skipped a few classes in the middle, but it should be more or less obvious what they contain.
    I then created a class called GeographyAdmin as follows:

    <?php
    class GeographyAdmin extends ModelAdmin {

    public static $managed_models = array('Region', 'Country', 'State', 'Province', 'City', 'District');
    static $url_segment = 'geography';
    static $menu_title = 'Geography';

    }

    So now I have a new main tab on the left side that allows me to add, remove and edit all these data objects and edit the relationships between them. However certain things are not working like I want it to:

    1 - When I select a city and add a district to it, I have to manually chose the region, country, state and province. Ideally the CMS user would not be able to choose any of these, the province should automatically be the same province as the city, the state the same state as the state of the province of the city, the country the same as the country of the state of the province of the city, and the region should be the same as the region assigned to the country that is assigned to the state that is assigned to the province that is assigned to the city.

    2- If there is a loose state, and I assign it to a country, all the provinces, cities and districts associated with the state should also get this association as well as the region association of the country.

    3- If I create a new loose district and I a chose the region, the only available countries should be countries associated with that region and when I chose the country the only available states should be those associated with the country that was chosen.

    Now I realize that I could solve this problem by reducing the complexity of the Dataobjects in question, so that only countries have a region (and only regions have countries), only states have a country (and vice versa), etc... This wouldn't work for me as all of the geographic Dataobjects also have many Hotel Dataobjects. Ultimately I absolutely need to implement the functionality I mentioned above for the has_many Hotels as I want to be able to retrieve all the hotels in a region, country, state, province, city or district with a single SQL query on the front end.

    But I am somewhat flustered by the task. I am not so sure where I would start, which methods I would have to overload, or even if there is a way to indicate to saphire or the cms that I need this kind of relationship and spare myself the hard work. If anyone can give me some pointers in this direction I would be grateful.

    Thanks.

    248 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.