UndefinedOffset / SortableGridField

Adds drag and drop functionality to Silverstripe's GridField
BSD 3-Clause "New" or "Revised" License
93 stars 62 forks source link

many_many ss 3.6 GridFieldAddNewMultiClass Column 'SortOrder' in field list is ambiguous #114

Closed nimeso closed 6 years ago

nimeso commented 6 years ago

I'm getting this error 'Column 'SortOrder' in field list is ambiguous'

Parent

<?php
class Quest extends DataObject{

    private static $db = array(
        "Title"            => "Varchar(256)"
                "SortOrder"   => "Int"
    );

    static $default_sort = "SortOrder ASC";

    private static $many_many = array(
        "Tasks"      => "Task"
    );

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

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

    public function getCMSFields(){

        $fields = parent::getCMSFields();

        $gridfieldPages = new GridField("Tasks","Tasks",$this->Tasks());
        $gridfieldPages->getConfig()
             ->addComponent(new GridFieldSortableHeader())
             ->addComponent(new GridFieldSortableRows("SortOrder"))
             ->addComponent(new GridFieldDetailForm())
             ->addComponent(new GridFieldEditButton())
             ->addComponent(new GridFieldDeleteAction())
             ->addComponent($multiClassPages = new GridFieldAddNewMultiClass());
        $multiClassPages->setClasses( array("TaskText","TaskArea","TaskUpload", "TaskImageUpload", "TaskCode", "TaskLink") );
        $fields->addFieldToTab("Root.Tasks", $gridfieldPages);
        return $fields;
    }

}

Child base class

<?php
class Task extends DataObject{

    private static $db = array(
        "Title" => "Varchar(256)"
    );

    private static $belongs_many_many= array(
        "Quests" => "Quest"
    );

    public function getCMSFields(){
        return $fields;
    }

}
UndefinedOffset commented 6 years ago

I was expecting that actually, either drop the column on the task table or use a different name in the many many extra fields :)

nimeso commented 6 years ago

Thanks heaps @UndefinedOffset sorry, I'm not quite following :( Thanks for all your help. could you explain a bit more or send me my code with the corrections?

UndefinedOffset commented 6 years ago

Your code is ok, the problem is the Task object (and it's database table) has a field called SortOrder, and your many many extra fields does as well. So when the queries try to run it sees two fields in the database. ao you need to resolve the conflict by either changing the name of your sort column in the many many extra fields to something unique or remove the field from the task class and database table if it's not needed. Either solution should resolve the ambitious column name issue. If I remember right it's a limitation of the orm in silverstripe 3.x where it won't prefix fields with their table name in some scenarios.

nimeso commented 6 years ago

Sorted! (see the pun there) thanks.