Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching subclasses of Page #1

Open
briceburg opened this issue Jan 7, 2015 · 3 comments
Open

Searching subclasses of Page #1

briceburg opened this issue Jan 7, 2015 · 3 comments

Comments

@briceburg
Copy link

Is it possible to search objects that extend Page? For instance;
I have a type, Article that extends Page and provides the following extra fields; Author and Summary. SilverStripe will create a database table named Article that containts the above fields as columns, but borrows upon the SiteTree table for the Content and Title field.

I want to be able to search by Author, Summary text, Content text, and Title.. and add the following to _config;

Searchable::add('Article',array('Author','Summary','Content','Title'),'News Articles');

This results in:

[User Error] Couldn't run query: ALTER TABLE "Article" ADD fulltext "SearchFields" ("Author","Summary","Content","Title") Key column 'Content' doesn't exist in table

As those [Content, Title] fields don't exist on the Article table.

Does the module support this scenario?

Many thanks for any ideas.

~ Brice

@mlewis-everley
Copy link
Member

I have encountered this issue as well, it is a scenario I would definitly like to support and I thought Silverstripe would automatically perform a join onto parent objects when filtering.

Apparently however this is not the case (or my code is incorrect in some way), so I am trying to work out how to get this working and any help would be greatly appreciated :-).

@briceburg
Copy link
Author

I'm in the midst of coding my own search routine. I handle therelationships by checking for the presence of "." in the search field, and only add fulltext indexes on fields that belong to the Class table. Here's a quick preview;

$searchClasses = array(
    'SiteTree' => array('Title','Content'),
    'Article' => array('Author','Summary','SiteTree.Title','SiteTree.Content'),
    'Attorney' => array('Email','SideContent','DirectPhone','DirectFax','SiteTree.Content','SiteTree.Title'),
    'Practice' => array('SiteTree.Title','SiteTree.Content'),
    'Region' => array('Summary','SiteTree.Title','SiteTree.Content'),
);

SearchableExtension::enable($searchClasses);
class SearchableExtension extends DataExtension
{

    public static $searchClasses = array();

    public static function enable($searchClasses = array())
    {
        foreach ($searchClasses as $class => $fields) {

            $tableFields = array_filter($fields,
                function ($var)
                {
                    return (strpos($var, '.') === false);
                });

            if (! empty($tableFields)) {
                //@todo ONLY if mysql version < 5.6
                Config::inst()->update($class, 'create_table_options',
                    array(
                        'MySQLDatabase' => 'ENGINE=MyISAM'
                    ));

                $tableFieldsStr = implode(',',
                    array_map(
                        function ($var)
                        {
                            return sprintf('"%s"', $var);
                        }, $tableFields));

                $class::add_extension("FulltextSearchable('$tableFieldsStr')");
            }
        }

        self::$searchClasses = $searchClasses;
    }

    public static function get_extra_config($class, $extensionClass, $args)
    {
        return array(
            'indexes' => array(
                'SearchFields' => array(
                    'type' => 'fulltext',
                    'name' => 'SearchFields',
                    'value' => $args[0]
                )
            )
        );
    }
}

@mlewis-everley
Copy link
Member

Ok, I think I can see what you are doing. It would be be a little less reliant on configuration if the enable method tried to automatically detect if the class provided is an extension and if the fields to search exist on that class or the parent...

Not sure how to though, will have to have a ponder...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants