Silverstripe Version: 4.2.1
PHP 7.0.8
Question:
I have a dataobject page that has a varchar(100) field called DropOffLocations for saving of CheckboxSetField selections.
The selections were getting saved in the database as comma delimited i.e. 1,2,3
Now suddenly the values are saving as [“1”,“2”,“3”].
But the weird thing is, on the development test server, same code, the values are getting stored as 1,2,3
This breaks a query using the following where statement:
$filter = "ID IN(".$getlocation->DropOffLocations.")";
$page = VehicleLocations::get()->where($filter);
Debug output:
FROM “VehicleLocations” WHERE (ID IN([“1”,“3”,“8”])) 42000-1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘[“1”,“3”,“8”]))’ at line 5
UPDATE:
Appears this is a issue from upgrading from silverstripe 3.x to silverstripe 4.x. Not a bug. In 4.x, data is saved as a JSON object. So a gotcha. Was only a issue when I updated checkbox selections…
See:
opened 12:51PM - 04 Apr 18 UTC
closed 01:05PM - 04 Apr 18 UTC
## Affected Version
4.1
## Description
I'm having issues with migrating… from SS3 to 4 and getting old database content to work. I have a varchar field which is edited using a CheckboxSetField where the editor can select several options.
In Silverstripe 4 CheckboxSetField saves data to the database in the format ["Option1","Option2"]. In Silverstripe 3, data was saved as a comma separated list like this: Option1,Option2.
The old format doesn't seem to work in SS4. I have a lot of existing content, and if I edit a page the checkboxes are all empty even though there are available data in the database. If I then save the page, without checking any of the checkboxes, the existing data is removed, which is a big problem.
The problem with saving in the format ["Option1","Option2"] is that I can't do a FIND_IN_SET SQL query against the column, as the expected format for that to work is a comma separated list.
Sure, I could run a script to update all existing content to the new format, but doing that I can't query the column (for example, if I wish to get all pages where "Option1" is selected).
For what it's worth, this is how I create the CheckboxSetField:
```
$statuses = [
"Now playing" => "Now playing",
"Upcoming" => "Upcoming",
"Archived" => "Archived"
];
$fields->addFieldToTab('Root.Show', CheckboxSetField::create("ShowStatus", "Status", $statuses));
```
My solution was to add relation object to save into.
LanceH
14 February 2023 02:12
2
This still seems to be an issue in version 4.7… 4 years later.
My case was a DataObject with a MultiEnum field.
CheckboxSetField after a post sends an array.
Field creation pulling options from the models $db
property.
CheckboxSetField::create('SomeField', 'Some field', singleton('TheDataObject')->dbObject('SomeField')->enumValues());
Saving in the form action of a controller:
$NewThing->SomeField = implode(',', array_keys($data['SomeField']));`
Quite annoying CheckboxSetField
doesn’t seem to know how to save itself.