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.

General Questions /

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

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

Working with $many_many relationship


Go to End


3 Posts   946 Views

Avatar
nzstephenf

Community Member, 63 Posts

3 June 2015 at 4:40pm

Hey guys,

I've been trying to create a tag system with article pages and one of the things I wanted to do is introduce a search by tag feature in my project. Here's what I've got:

class ArticlePage extends Page {
    private static $many_many = array(
        "ArticleTags" => "ArticleTags"
    );
}

class ArticleTags extends DataObject{
    private static $db = array(
        "Title" => "Varchar(200)"
    );

    private static $belongs_many_many = array(
        "ArticlePages" => "ArticlePage"
    );

I have the tagfield module installed to make things easier in the backend.

How would I go about creating within a page controller a way to use ORM, HTTP Request (so SS_HTTPRequest to getParam) and work with many_many objects (ArticleTags and ArticlePage). Never been able to crack many_many things.

Cheers,
Stephen

Avatar
Pyromanik

Community Member, 419 Posts

4 June 2015 at 8:26am

You have a choice to use either fulltext, or the more reliable LIKE.

Then you hook a simple search form up to an action that performs something along the lines of: ArticleTag::get()->filter(<secrets within>)->ArticlePages() ... maybe. Maybe more like (if that doesn't work, it might not):

ArticlePage::get()->byIDs(ArticleTag::get()->filter(<secrets within>)->getIDList())

Avatar
nzstephenf

Community Member, 63 Posts

22 June 2015 at 9:52pm

Hey, I never ended up having a reply go through on this post. Thanks for your suggestion anyways Pyromanik!
Ended up going with the alternative option of not using the $many_many method of tagging articles and using the static $db Text method.