Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS 2.4 DataObject write() writes 'NULL' to DB

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

Page: 1
Go to End
Author Topic: 440 Views
  • BlackHawk
    Community Member
    7 Posts

    SS 2.4 DataObject write() writes 'NULL' to DB Link to this post

    Hi @ all,

    I have problems to write the value of a object variable to the database field.

    This is the code of the dataobject (I am sorry for the code-comments in german, but I guess you know, what it is about):

    class TestCode extends DataObject {
       protected $Code;
       static $singular_name = 'Code';
       static $plural_name = 'Codes';
       static $db = array (
          'Code' => 'Varchar'
       static $has_one = array (
       public function __construct() {
          $this->Code = $this->generateUniqueCode();
       protected function generateCode() {
           * Anzahl der Zeichen des Codes
           * Bedingung: $length <= count($chars)
          $length = 12;
          //Zeichen die für den Code verwendet werden sollen
          $chars = array_merge(
          //Array in das die Zeichen aufgenommen werden sollen
          $codeArr = array();
          //Zufallszahlengenerator mit seed setzen
           * Das Array mit Zahlen füllen:
           * Für jeden Schleifendurchlauf der äußeren Schleife
           * wird immer ein neuer Buchstabe zufällig aus der inneren Schleife gewählt
          for($i = 0; $i < count($chars); $i++){
             for($j = 1; $j <= (count($chars)*2); $j++){
                $swap = mt_rand(0, count($chars)-1);
                $tmp = $chars[$swap];
                $chars[$swap] = $chars[0];
                $chars[0] = $tmp;
             $codeArr[$i] = $chars[0];
          //generierter Code
          $code = substr(implode('', $codeArr),0,$length);
          return $code;
       protected function questionDbForCode($code) {
          $sqlQuery = new SQLQuery();
          $sqlQuery->where("Code", $code);
          $val = $sqlQuery->execute()->value();
          if($val) {
             return true;
          } else {
             return false;
       protected function generateUniqueCode() {
          $code = '';
          $goOn = false;
          do {
             $code = TestCode::generateCode();
             $goOn = TestCode::questionDbForCode($code);
          } while ($goOn);
          return $code;
       function getCode() {
          return $this->Code;

    So when a new instance of 'TestCode' is generated, the objectvariable will get a code.
    Now in another class I am calling the function 'generateCode()'. This generates a new databaseentry but the value for the column 'Code' is NULL.

    Here is the code for the function 'generateCode()'

    function generateCode() {
    $tcode = new TestCode();

    I guess there is no association between the objectvariable '$Code' and the database field 'Code'.
    What am I doing wrong?
    Anybody an idea?

    Thank's a lot!

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.