Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Can't add new DataObject with property of type 'Currency'


Go to End


2 Posts   1742 Views

Avatar
Erin

Community Member, 26 Posts

31 May 2010 at 12:03pm

Edited: 31/05/2010 12:06pm

Hi guys!
I'm having a problem that I'm hoping someone here can help me with.

First the basics:
I have a dataobject of type Item. Item has 3 static properties - Name(Text), Description(HTMLText) and Price(Currency).

class Item extends DataObject{
	static $db = array(
		'Name' => 'Text',
		'Description' => 'HTMLText',
		'Price' => 'Currency'
	);
 	static $defaults = array(
		'Name' => 'Hamburger',
		'Price' => 4.99
	);	
	static $has_one = array(
		'ItemCategories' => 'ItemCategory'
	);
	function getCMSFields_forPopup() {
		$fields = new FieldSet();
		$fields->push( new TextField('Name'));
		$fields->push( new TextareaField('Description'));
		$fields->push( new CurrencyField('Price'));
		return $fields;
	}
}

When I try to add a new Item, it errors, like this:

[User Error] Couldn't run query: INSERT INTO `Item` SET Created = NOW() Field 'Price' doesn't have a default value
POST /bbd/s/admin/cms/EditForm/field/Meals/item/2/DetailForm/field/ItemCategories/item/3/DetailForm/field/Items/AddForm?ctf[Items][start]=0&ctf[Items][per_page]=10&ctf[Items][showall]=0&ctf[Items][sort]=Created&ctf[Items][sort_dir]=DESC&ctf[Items][search]=&ctf[Items][filter]=

Line 401 in C:\****\bbd\s\sapphire\core\model\MySQLDatabase.php
Source

392 	}
393 	
394 	function databaseError($msg, $errorLevel = E_USER_ERROR) {
395 		// try to extract and format query
396 		if(preg_match('/Couldn\'t run query: ([^\|]*)\|\s*(.*)/', $msg, $matches)) {
397 			$formatter = new SQLFormatter();
398 			$msg = "Couldn't run query: \n" . $formatter->formatPlain($matches[1]) . "\n\n" . $matches[2];
399 		}
400 		
401 		user_error($msg, $errorLevel);
402 	}
403 }
404 
405 /**
406  * A result-set from a MySQL database.
407  * @package sapphire

Trace

    * Couldn't run query: INSERT INTO `Item` SET Created = NOW() Field 'Price' doesn't have a default value
      Line 401 of MySQLDatabase.php

When I change the Price's type from Currency to Text, I can add a new Item without problems. I can also update existing items without error, whether the type is Currency or Text.

Can I anyone give me an idea what I'm missing in all this?

Avatar
mark_s

Community Member, 78 Posts

3 June 2010 at 11:24pm

Hi.

What database are you using? And what code are you using to create the Item?

Mark