Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Can't add new DataObject with property of type 'Currency'

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

Page: 1
Go to End
Author Topic: 1167 Views
  • Erin
    Avatar
    Community Member
    26 Posts

    Can't add new DataObject with property of type 'Currency' Link to this post

    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?

  • mark_s
    Avatar
    Community Member
    78 Posts

    Re: Can't add new DataObject with property of type 'Currency' Link to this post

    Hi.

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

    Mark

    1167 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.