swipestripe / silverstripe-swipestripe-category

Product categories extension for SwipeStripe
1 stars 10 forks source link

GridField editing relations to products #2

Open samthejarvis opened 10 years ago

samthejarvis commented 10 years ago

Latest SS3.1, apparent in fresh installs w/ latest categories module Using a GridField to manage a relation between a Page and Product, getting:

Fatal error: Call to a member function stat() on a non-object in C:\wamp\www\framework\model\DataObject.php on line 3226

Removing the searchable_fields from ProductCategory_Extension fixed this as calling ->relObject('Category') on a Product returns null, can't figure out why.

Changing 'Category' in searchable_fields to 'ProductCategories' (the actual name of the belongs_many_many relation) throws the same error.

Any ideas?

frankmullenger commented 10 years ago

Hi Sam, thanks for reporting this. Not entirely sure what the issue might be, which version of Silverstripe are you using exactly? Also, do you have a code snippet?

frankmullenger commented 10 years ago

Hi @samthejarvis, I've tested SwipeStripe with 3.1.2 and categories appear to be working for me on Product pages. Have not tried in GridField but this issue may be more closely related to GridField, at this point I'm not sure what the issue might be - could be a good idea to ask on IRC or the forums. Will close for now.

threesquared commented 10 years ago

Getting this same error when trying to manage a Product to Product relationship with GridField. Removing the searchable_fields from ProductCategory_Extension fixed it.

stevie-mayhew commented 10 years ago

Yeah we have had this problem as well, I've also had to remove the searchable fields. @frankmullenger can you reopen this one?

frankmullenger commented 10 years ago

Guess this needs a little more investigation :smile:

dhensby commented 10 years ago

+1 I'm getting this too

It appears to be an issue with the ProductCategory_Extension::$searchable_fields

It works if I change it to:

    public static $searchable_fields = array(
-       'Category' => array(
+       'ProductCategories.Title' => array(
            'field' => 'TextField',
            'filter' => 'ProductCategory_SearchFilter',
            'title' => 'Category'
        )
    );

Edit: You can replicate this by setting up a many_many with a Product and some other DataObject and the automatic CMSFields that are generated cause the error

dhensby commented 10 years ago

A very nasty workaround in the mean-time is:

mysite/_config.php

$searchableFields = Config::inst()->get('Product', 'searchable_fields');
$searchableFields['ProductCategories.Title'] = $searchableFields['Category'];
unset($searchableFields['Category']);
Config::inst()->remove('Product', 'searchable_fields');
Config::inst()->update('Product', 'searchable_fields', $searchableFields);
unset($searchableFields);
dhensby commented 10 years ago

This "fix" actually causes another problem with the GridFieldAddExistingAutocompleter.

So if you want to use the GridFieldAddExistingAutocompleter to relate Products to another object the SQL query generated has no "from" clause defined.

This is something to do with the apply function in ProductCategory_SearchFilter as it's using SiteTree_Live and doing an inner join. This appears to be knocking the SiteTree table out of the from clause and into a join.

When I do this in my config, it all works as expected:

$searchableFields = Config::inst()->get('Product', 'searchable_fields');
//$searchableFields['ProductCategories.Title'] = $searchableFields['Category'];
unset($searchableFields['Category']);
Config::inst()->remove('Product', 'searchable_fields');
Config::inst()->update('Product', 'searchable_fields', $searchableFields);
unset($searchableFields);
stevie-mayhew commented 10 years ago

We should also get https://github.com/swipestripe/silverstripe-swipestripe-category/pull/1 merged into the 2.1 branch, as well as change the public declarations within the DataExtension to private.

frankmullenger commented 10 years ago

"You can replicate this by setting up a many_many with a Product and some other DataObject and the automatic CMSFields that are generated cause the error" - I still couldn't replicate for some reason, does anyone have some boilerplate code for replicating this issue?

dhensby commented 10 years ago

@frankmullenger

class Material extends DataObject {
    private static
        $db = array(
            'Title' => 'Varchar(255)',
        ),
        $many_many = array(
            'Products' => 'Product',
        );
}

class ProductExtension extends DataExtension {
    private static $belongs_many_many = array(
        'Materials' => 'Material',
    );
}

This is the basic idea. If you go to the CMS to create a Material and then try to use the auto complete field on the grid for managing the Material -> Product relation it will error.

anhld commented 10 years ago

I am getting same error here, sample code from @dhensby , SS 3.1.5. Is it able to fix?