Barry
Barry

Reputation: 3318

Silverstripe $searchable_fields over a $has_one, then a $many_many relationship

In Silverstripe V2 it was possible to create searchable fields that spanned across relationships such as Obj1.Obj2.Obj3.Obj3Field but now in V3 this produces the following error...

[User Error] Uncaught LogicException: relation can't be called on an UnsavedRelationList.
GET /zz_silverstripe_default/admin/mymodeladmin/
Line 391 in C:\xampp\htdocs\zz_silverstripe_default\framework\model\UnsavedRelationList.php
Source
390     public function relation() {
391         throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
392     }
Trace
UnsavedRelationList->relation(EndObject) 
DataObject.php:3009
DataObject->relObject(MiddleDataObjects.EndObject.Name) 
DataObject.php:2285
DataObject->scaffoldSearchFields() 
DataObject.php:2230
DataObject->getDefaultSearchContext() 
ModelAdmin.php:173

The code to reproduce this is...

class MyModelAdmin extends ModelAdmin {
    static $managed_models = array('RootDataObject');
    static $url_segment = 'mymodeladmin';
    static $menu_title = 'MyModelAdmin';
    static $model_importers = array();
}

class RootDataObject extends DataObject {
    private static $db = array('Name' => 'Varchar(255)');
    private static $has_many = array('MiddleDataObjects' => 'MiddleDataObject');
    static $searchable_fields = array('MiddleDataObjects.EndObject.Name');
}

class MiddleDataObject extends DataObject {
    private static $db = array('Name' => 'Varchar(255)');
    private static $has_one = array('RootDataObject' => 'RootDataObject');
    private static $many_many = array('EndDataObjects' => 'EndDataObject');
}

class EndDataObject extends DataObject {
    private static $db = array('Name' => 'Varchar(255)');
    private static $belongs_many_many = array('OtherDataObjects' => 'OtherDataObject');
}

I've uploaded this code to http://pastebin.com/QBfU8Fub with more white space and some php tags to make it easier to reproduce this issue. I'm after essentially the previous behaviour to work so I do not have to keep writing search contexts and filters manually to do something I've come to expect from this nice CMS.

Upvotes: 2

Views: 474

Answers (1)

3dgoo
3dgoo

Reputation: 15794

This is a bug in the UnsavedRelationList object in the SilverStripe framework code.

Here is another user with this issue: https://github.com/silverstripe/silverstripe-widgets/issues/123

There is a fix for this: https://github.com/silverstripe/silverstripe-framework/pull/5348

However, this update won't be released until SilverStripe 3.4 / SilverStripe 4.0.

Upvotes: 3

Related Questions