SortableGridField导致SQL错误

时间:2013-06-21 01:18:50

标签: silverstripe

经过大量搜索后,我找到了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 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

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

只需将SortOrder字段保存在关系表上,方法是将其添加到TwoColumn:

private static $many_many_extraFields = array(
        'SideBars' => array(
            'SortOrder' => 'Int'
        )
    );

答案 3 :(得分:0)

对于那些在从数据库转储相关表后仍然遇到同样问题的人,请确保您没有设置任何' SortOrder' &$ 39; $ belongs_many_many'上的字段关系的类。 按照此模块的示例,通过设置' $ many_many_extraFields'来创建此字段。在&$ 39; $ many_many'类。

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

/*** 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'
    );
}

你可以看到&#39; TestObject&#39;不再定义&#39; SortOrder&#39;字段,但它现在定义在&many; $ many_many_extraFields&#39; $ many_many_extraFields&#39;方法。 如果&#39; SortOrder&#39;字段定义为&#39; $ db&#39;此外,数据库将有两个&#39; SortOrder&#39;此关系的字段,导致上述错误。