Beispiel #1
0
 /**
  * Add new fields to table, array schema like in create() function
  * @param array $fields Associative array
  */
 public function addFields(array $fields)
 {
     $fields = Helpers\Validate::arrToLower($fields);
     Helpers\Validate::types(array_values($fields));
     $schema = $this->schema();
     $fields = array_diff_assoc($fields, $schema);
     if (!empty($fields)) {
         $config = $this->config();
         $config->schema = array_merge($schema, $fields);
         $data = $this->getData();
         foreach ($data as $key => $object) {
             foreach ($fields as $name => $type) {
                 if (Helpers\Validate::isNumeric($type)) {
                     $data[$key]->{$name} = 0;
                 } else {
                     $data[$key]->{$name} = null;
                 }
             }
         }
         Helpers\Data::table($this->name)->put($data);
         Helpers\Config::table($this->name)->put($config);
     }
 }