Hi, I'm writing something that allows the CMS user to select products and bundle them together into kits.
The idea would be to have a flag on each product to say whether it's a kit of products or a single product. And if it's a kit of products, I'd like to save their ID numbers to an array (or an nested table)
I don't think I can save an array to MySql, so I'd like to have a text field where the product IDs are stored as a string of comma separated values.
class ProductsPage extends Page {
static $db = array(
.
.
.
'Is_Kit' => 'Boolean',
'Kit_Elements' => 'Text'
);
function getCMSFields() {
.
.
.
$fields->addFieldToTab("Root.Content.KitElements", new ListboxField(
$name = 'Kit_Elements',
$title ="Kit Elements",
$source = $productsMap,
'',
$size=4,
$multiple = true
)
);
The list, when displayed in the CMS, lets me select multiple values but as soon as I try to save, I get an "error while saving content" message.
I have also tried to put some code in onAfterWrite(), the idea being that, if the save is not automatic, then I could go back, grab the selected items from the list and write them to the DB column myself, but that doesn't work for me either.
function onAfterWrite(){
parent::onAfterWrite();
$ProductList = null;
$Product = DataObject::get_by_id("ProductsPage", $this->ID);
while (list ($key, $val) = each ($this->Kit_Elements)) {
$ProductList .= $val . ",";
}
$Product->Kit_Elements = $ProductList;
$Product->write();
}
What can I do? Or what am I doing wrong?
EDIT: There were a couple of typos which I fixed. But I still get the "Error saving content" message.