Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Finding Siblings by Tags (ORM Question)


Go to End


2 Posts   1560 Views

Avatar
Andre

Community Member, 146 Posts

8 October 2015 at 4:21am

Hi, I have follwoing models:

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

    private static $many_many = array(
        'Tags' => 'Tag'
    );
}

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

    private static $belongs_many_many = array(
        'Items' => 'Item'
    );
}

How can I receive all Siblings of an Item by its Tags? Is this Possible in one Request, to make it paginatable?

An inelegant way would be to first get all Tag IDs of an Item's Tags and then get all Items by these Tag IDs excluding my Parent Firswt Item.

Kind regards

andre

Avatar
swaiba

Forum Moderator, 1899 Posts

10 October 2015 at 3:17am

Edited: 10/10/2015 3:19am

Well I don't see another choice but to do the inelegant way...

$siblingItems = Item::get()
	->leftJoin('Item_Tag','Item_Tag.ItemID = Item.ID')
	->filter('Item_Tag.TagID',$myItem->Tags()->Column('ID'))
	->exclude('Item.ID',$myItem->ID);

(note untested, I might have the Item_Tag round the wrong way)