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.

Data Model Questions /

SS 2.4 DataObject write() writes 'NULL' to DB




Community Member, 7 Posts

4 September 2012 at 4:58am

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!