Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Fetching Objects by their many_many relations

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 472 Views
  • Andre
    Avatar
    Community Member
    126 Posts

    Fetching Objects by their many_many relations Link to this post

    Hi there,

    I'm trying to fetch some objects by their many_many relations.

    The Dataobjects look the following:

    class Asset extends DataObject {
    static $db = array(
    'Title' => 'Varchar',
    'IsActive' => 'Boolean'
    );

    static $many_many = array(
    'Countries' => 'Country',
    'Languages' => 'Language'
    );
    }

    class Language extends DataObject {
    static $db = array(
    'Title' => 'Varchar'
    );

    static $belongs_many_many = array(
    'Assets' => 'Asset'
    );
    }

    class Country extends DataObject {
    static $db = array(
    'Title' => 'Varchar'
    );

    static $belongs_many_many = array(
    'Assets' => 'Asset'
    );
    }

    Countries and Languages will be a smaller number, about 20-40 each. But Assets there will be up to a few 1000.

    Now I want to get all Assets which do exist for a selected number of Countries and Languages. The Selections should work the following. If an Asset exists for the selected Countries, but not for the selected Languages, it should not been chosen. Only if the Asset matches at least one of the selected countries AND one of the selected languages.

    How do I get these Assets at best. One Idea is, to fetch all Asset IDs by country, then fetch from these IDs by language and then fetch all Dataobjects with the left IDs. Another would be a left join, which I'm unable to create for the moment.

    What would be the best way under the aspect of performance? And How do I fetch only the IDs of a belongs_many_many relation without querying the Database to much?

    thanks for your advices

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