0

经过大量搜索,我发现:https ://github.com/UndefinedOffset/SortableGridField/blob/master/docs/ManyManyExample.md的用法示例。

但是,实现此代码会导致:

[用户错误] 无法运行查询:SELECT DISTINCT MAX("SortOrder") FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"." TwoColumnID" = '2') 字段列表中的列 'SortOrder' 不明确

我正在使用 SS3 并安装了 SortableGridField(尽管在运行 dev/build/?flush=all 时没有消息表明安装成功)。

关闭开发模式时,CMS 不会加载(“服务器错误”),但是启用开发模式后,我会收到上述 SQL 错误。无论如何,它似乎工作正常,我只需要找出引发此错误的原因。

更新

以下是此错误生成的堆栈跟踪的顶部(页面上有 2 个 [User Error] 蓝色标题):

[User Error] Couldn't run query: SELECT DISTINCT MAX("SortOrder") FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') Column 'SortOrder' in field list is ambiguous
Couldn't run query: SELECT DISTINCT MAX("SortOrder") FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') Column 'SortOrder' in field list is ambiguous 
MySQLDatabase.php:580
MySQLDatabase->databaseError(Couldn't run query: SELECT DISTINCT MAX("SortOrder") FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') | Column 'SortOrder' in field list is ambiguous,256) 
MySQLDatabase.php:132
MySQLDatabase->query(SELECT DISTINCT MAX("SortOrder") FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2'),256) 
DB.php:200
DB::query(SELECT DISTINCT MAX("SortOrder") FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2'),256) 
SQLQuery.php:949
SQLQuery->execute() 
DataQuery.php:372
DataQuery->aggregate(MAX("SortOrder")) 
DataQuery.php:338
DataQuery->max(SortOrder) 
DataList.php:676
DataList->max(SortOrder) 
GridFieldSortableRows.php:148
GridFieldSortableRows->fixSortColumn(GridField,ManyManyList) 
GridFieldSortableRows.php:99
GridFieldSortableRows->getManipulatedData(GridField,ManyManyList) 
GridField.php:215
GridField->getManipulatedList() 
GridField.php:255
GridField->FieldHolder() 
...

[User Error] Couldn't run query: SELECT DISTINCT count(DISTINCT "Sidebar"."ID") AS "0" FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') AND ("SortOrder"=0) Column 'SortOrder' in where clause is ambiguous

Couldn't run query: SELECT DISTINCT count(DISTINCT "Sidebar"."ID") AS "0" FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') AND ("SortOrder"=0) Column 'SortOrder' in where clause is ambiguous 
MySQLDatabase.php:580
MySQLDatabase->databaseError(Couldn't run query: SELECT DISTINCT count(DISTINCT "Sidebar"."ID") AS "0" FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') AND ("SortOrder"=0) | Column 'SortOrder' in where clause is ambiguous,256) 
MySQLDatabase.php:132
MySQLDatabase->query(SELECT DISTINCT count(DISTINCT "Sidebar"."ID") AS "0" FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') AND ("SortOrder"=0),256) 
DB.php:200
DB::query(SELECT DISTINCT count(DISTINCT "Sidebar"."ID") AS "0" FROM "Sidebar" INNER JOIN "TwoColumn_Sidebars" ON "TwoColumn_Sidebars"."SidebarID" = "Sidebar"."ID" WHERE ("TwoColumn_Sidebars"."TwoColumnID" = '2') AND ("SortOrder"=0),256) 
SQLQuery.php:949
SQLQuery->execute() 
SQLQuery.php:1054
SQLQuery->count(DISTINCT "Sidebar"."ID") 
DataQuery.php:329
DataQuery->count() 
DataList.php:666
DataList->count() 
GridFieldSortableRows.php:150
GridFieldSortableRows->fixSortColumn(GridField,ManyManyList) 
GridFieldSortableRows.php:99
GridFieldSortableRows->getManipulatedData(GridField,ManyManyList) 
GridField.php:215
GridField->getManipulatedList() 
GridField.php:255
GridField->FieldHolder() 
ViewableData.php:366
ViewableData->obj(FieldHolder,,,1) 
...
4

4 回答 4

3

对于遇到此问题的任何人,可以通过转储您的 DataObject 和 many_many 关系表(即 SideBar 和 TwoColumn_SideBar 表)来解决它。两个表都可能有一个 SortOrder 列,而 mysql 查询无法确定您想要哪一个。

于 2014-01-08T01:58:28.763 回答
1

我遇到过这个问题,它是由重复的 SortOrder 列引起的。

您可以手动删除 Sidebar DataObject 表上的 SortOrder 列,并从类中删除该字段。

通过将其添加到 TwoColumn 仅保留关系表上的 SortOrder 字段:

private static $many_many_extraFields = array(
        'SideBars' => array(
            'SortOrder' => 'Int'
        )
    );
于 2015-04-12T21:57:16.390 回答
1

在我遇到 SortableGridField 问题后,我使用了 ajshorts gridfieldextensions。 https://github.com/ajshort/silverstripe-gridfieldextensions

于 2013-06-21T14:54:11.703 回答
0

对于从数据库中转储相关表后仍然遇到同样问题的人,请确保您没有在关系的“$belongs_many_many”类上设置任何“SortOrder”字段。按照这个模块的例子,这个字段是通过在“$many_many”类中设置“$many_many_extraFields”来创建的。

更新: 不同之处在于您正在创建的关系类型。在模块本身的示例中,您可以看到,对于has_many 关系,第二个对象 (TestObject) 仍然明确定义了“SortOrder”字段。

/*** TestPage.php ***/
class TestPage extends Page {
    public static $has_many=array(
        'TestObjects'=>'TestObject'
    );
}


/*** TestObject.php ***/
class TestObject extends DataObject {
    public static $db=array(
        'Title'=>'Text',
        'SortOrder'=>'Int'
    );

    public static $has_one=array(
        'Parent'=>'TestPage'
    );

    public static $default_sort='SortOrder';
}

然而,对于many_many 关系,该字段不是为类 (TestObject) 显式定义的,而是在对象关系上定义的。

/*** TestPage.php ***/
class TestPage extends Page {
    public static $many_many=array(
        'TestObjects'=>'TestObject'
    );

    public static $many_many_extraFields=array(
        'TestObjects'=>array(
            'SortOrder'=>'Int'
        )
    );

    public function TestObjects() {
       return $this->getManyManyComponents('TestObjects')->sort('SortOrder');
    }
}


/*** TestObject.php ***/
class TestObject extends DataObject {
    public static $db=array(
        'Title'=>'Text'
    );

    public static $belongs_many_many=array(
        'TestPages'=>'TestPage'
    );
}

您可以看到“TestObject”不再定义“SortOrder”字段,而是现在在“$many_many_extraFields”方法中的关系上定义。如果 'SortOrder' 字段也定义为 '$db' 中,则数据库将有两个用于此关系的 'SortOrder' 字段,从而导致上述错误。

于 2014-09-17T09:50:56.480 回答