Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting fails when saving on parent Save in 2.4.5 #9

Closed
briceburg opened this issue Mar 1, 2011 · 1 comment
Closed

Sorting fails when saving on parent Save in 2.4.5 #9

briceburg opened this issue Mar 1, 2011 · 1 comment
Labels

Comments

@briceburg
Copy link

I have a has_many relationship between Page and Album. This looks like:

class Page extends SiteTree {

    public static $db = array(
    );

    public static $has_many = array(
        'Albums' => 'Album'
    );

    public function getCMSFields(){
        $fields = parent::getCMSFields();

        $field = new HasManyPickerField($this, 'Albums');
        $field->setOption('Sortable', true);

        $fields->addFieldToTab('Root.Content.Main', $field);

        return $fields;
    }

}

...

class Album extends DataObject {

    static $db = array('Title' => 'Varchar', 'Sort' => 'Int');

    static $has_one = array('Page' => 'Page');

}

When I go to save a MySQL Exception is thrown (related to update/select from the same table). A full trace is at bottom of this ticket. Error is:
user_error(Couldn't run query: UPDATE "Album" SET "Sort" = SELECT MAX("Sort") + 1 FROM "Album") WHERE "ID" = 1

You can't specify target table 'Album' for update in FROM clause,256)
line 525 of MySQLDatabase.php

Another issue; If "Sort" is not a field on album, I get the following exception:
Uncaught Exception: Object->__call(): the method 'hasowntabledatabasefield' does not exist on 'ViewableData'

I really like the simplicity of this module and am wondering if you think it worth my effort to extend the SortWeight (https://github.com/briceburg/silverstripe-sortweight) module to work with ItemSetField?

The goal of the module is to provide transparent drag-n-drop support so that component calls (Dataobject.getComponents|getManyManyComponents) are properly augmented without the developers explicitly setting the sort column. Admitetly I'm currently working out some kinks to make the transparency robust/reliable... but think I'll have them worked out soon.

Thanks!

Trace:
user_error(Couldn't run query:
UPDATE "Album" SET "Sort" =
SELECT MAX("Sort") + 1
FROM "Album")
WHERE "ID" = 1

You can't specify target table 'Album' for update in FROM clause,256)
line 525 of MySQLDatabase.php

MySQLDatabase->databaseError(Couldn't run query: UPDATE "Album" SET "Sort" = (SELECT MAX("Sort") + 1 FROM "Album") WHERE "ID" = 1 | You can't specify target table 'Album' for update in FROM clause,256)
line 123 of MySQLDatabase.php

MySQLDatabase->query(UPDATE "Album" SET "Sort" = (SELECT MAX("Sort") + 1 FROM "Album") WHERE "ID" = 1,256)
line 129 of DB.php

DB::query(UPDATE "Album" SET "Sort" = (SELECT MAX("Sort") + 1 FROM "Album") WHERE "ID" = 1)
line 122 of HasManyPickerField.php

HasManyPickerField->saveInto(Page)
line 1021 of Form.php

Form->saveInto(Page,1)
line 683 of LeftAndMain.php

LeftAndMain->save(Array,Form,SS_HTTPRequest)
line 329 of Form.php

Form->httpSubmission(SS_HTTPRequest)
line 143 of RequestHandler.php

RequestHandler->handleRequest(SS_HTTPRequest)
line 161 of RequestHandler.php

RequestHandler->handleRequest(SS_HTTPRequest)
line 147 of Controller.php

Controller->handleRequest(SS_HTTPRequest)
line 282 of Director.php

Director::handleRequest(SS_HTTPRequest,Session)
line 125 of Director.php

Director::direct(/admin/EditForm)
line 127 of main.php
@ajshort
Copy link
Owner

ajshort commented Mar 21, 2011

BUGFIX: Updated the Sort value population query to use two separate queries to support mysql. Closed by f1b2502.

@ajshort ajshort closed this as completed Mar 21, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants