Example #1
0
 /**
  * Show the form.
  * 
  * @param DBEntity $obj the enity to edit.
  *
  */
 public function showForm($obj)
 {
     $form = new BootstrapForm($obj);
     $ret = $form->open();
     $columns = $obj->getColumns();
     foreach ($columns as $k => $col) {
         $read_only = $col->isSequence() || $col->isAutomatic();
         $label = $this->getLabel($k, $col);
         $ret .= $form->new_group($k);
         $ret .= $form->label($label);
         $placeholder = null;
         if ($read_only) {
             if ($col->isSequence() && !$obj->_isPersistent) {
                 $placeholder = "The ID will be set by the database.";
             }
             $ret .= $form->input_disabled($k, $placeholder);
         } else {
             if ($col->getType() == DBColumn::VARCHAR) {
                 // A simple input
                 $ret .= $form->input_text($k);
             } else {
                 if ($col->getType() == DBColumn::INTEGER || $col->getType() == DBColumn::NUMERIC) {
                     // A simple input
                     $ret .= $form->input_number($k);
                 }
             }
         }
         $data = [];
         $data["property"] = $k;
         $data["colname"] = $col->getName();
         $data["desc"] = $col->getDescription();
         $data["label"] = $this->getLabel($k, $col);
         $data["type"] = $col->getTypeAsString();
         $ret .= "<hr>\n";
     }
     $ret .= $form->hidden("_entity", get_class($obj));
     $buttons = ['submit' => $obj->_isPersistent ? "Update" : "Insert"];
     if ($obj->_isPersistent) {
         $buttons['delete'] = "Delete";
     }
     $ret .= $form->submit_buttons($buttons);
     $ret .= $form->close();
     echo $ret;
 }
Example #2
0
 /**
  * Convert a SQL record to an entity.
  * 
  * NOTE: this method uses the $obj to store data in
  * it.
  *
  * @param DBEntity $obj an entity that will be modified.
  * @param array $row an associative array containing the column
  * names as keys and their respective values.
  *
  */
 public function sql2entity(&$obj, $row)
 {
     $definitions = $obj->getColumns();
     foreach ($definitions as $prop => $def) {
         $val = $row[$def->getName()];
         if (isset($val)) {
             $obj->{$prop} = $this->converter->fromSql($def, $val);
             // echo "CLASS=" . get_class($obj) . "; V= $val; P=$prop => SQL= {$this->$prop}\n";
         } else {
             // Set to NULL to avoid issues on non definied properties.
             $obj->{$prop} = null;
         }
     }
     return $obj;
 }