Example #1
0
 /**
  * Instantiate the row, checking to ensure the data array contains only
  * those columns present in the table's metadata.
  *
  * @param Table $table
  * @param array $data
  */
 public function __construct(Table $table, array $data = array())
 {
     $this->table = $table;
     $this->data = $data;
     $this->columns = $this->table->getRowColumns();
     // Apply defaults for new rows
     if (!count($this->data)) {
         foreach ($this->columns as $column) {
             $columnMetadata = $this->table->getMetadata('columns', $column);
             $default = $columnMetadata['DEFAULT'];
             // We skip temporal fields to avoid problems with certain defaults (e.g., CURRENT_TIMESTAMP, NOW())
             if (!in_array($columnMetadata['GENERIC_TYPE'], ['date', 'time', 'timestamp']) && null !== $default) {
                 $this->data[$column] = $default;
             }
         }
     }
     // Unset any data values not present in the columns array
     foreach ($this->data as $column => $value) {
         if (!in_array($column, $this->columns)) {
             unset($this->data[$column]);
         }
         if ($this->table->hasManyToManyRelationship($column)) {
             $this->virtualFieldsInitialized[] = $column;
         } elseif ($this->table->hasEav() && $this->table->getEav()->hasAttribute($column)) {
             $this->virtualFieldsInitialized[] = $column;
         }
     }
     $this->init();
 }
Example #2
0
 /**
  * Instantiate the row, checking to ensure the data array contains only
  * those columns present in the table's metadata.
  *
  * @param Table $table
  * @param array $data
  */
 public function __construct(Table $table, array $data = array())
 {
     $this->table = $table;
     $this->data = $data;
     $this->columns = $this->table->getRowColumns();
     // Apply defaults for new rows
     if (!count($this->data)) {
         foreach ($this->columns as $column) {
             $default = $this->table->getMetadata('columns', $column)['DEFAULT'];
             if (null !== $default) {
                 $this->data[$column] = $default;
             }
         }
     }
     // Unset any data values not present in the columns array
     foreach ($this->data as $column => $value) {
         if (!in_array($column, $this->columns)) {
             unset($this->data[$column]);
         }
         if ($this->table->hasManyToManyRelationship($column)) {
             $this->virtualFieldsInitialized[] = $column;
         } elseif ($this->table->hasEav() && $this->table->getEav()->hasAttribute($column)) {
             $this->virtualFieldsInitialized[] = $column;
         }
     }
     $this->initialData = $this->data;
     $this->init();
 }