public function addOrUpdateRecordByArray(array $recordData, $tableName = null)
 {
     $tableName = is_null($tableName) ? $this->table : $tableName;
     foreach ($recordData as $columnName => $columnValue) {
         if (is_array($columnValue)) {
             // $table = is_null($tableName) ? $this->table : $tableName;
             throw new SuppliedArrayAsColumnValue('Attempting to write an array as the value for column `' . $tableName . '`.`' . $columnName . '.');
         }
     }
     $columns = TableSchema::getAllNonAliasTableColumns($tableName);
     $recordData = SchemaManager::parseRecordValuesByType($recordData, $columns);
     $TableGateway = new self($this->acl, $tableName, $this->adapter);
     $rowExists = isset($recordData[$TableGateway->primaryKeyFieldName]);
     if ($rowExists) {
         $Update = new Update($tableName);
         $Update->set($recordData);
         $Update->where([$TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]]);
         $TableGateway->updateWith($Update);
         $this->emitter->run('postUpdate', [$TableGateway, $recordData, $this->adapter, $this->acl]);
     } else {
         $d = $recordData;
         if ($tableName == 'directus_files') {
             unset($d['data']);
             $d['user'] = Auth::getUserInfo('id');
         }
         $TableGateway->insert($d);
         $recordData[$TableGateway->primaryKeyFieldName] = $TableGateway->getLastInsertValue();
         if ($tableName == 'directus_files') {
             $Files = new \Directus\Files\Files();
             $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION);
             $thumbnailPath = 'thumbs/THUMB_' . $recordData['name'];
             if ($Files->exists($thumbnailPath)) {
                 $Files->rename($thumbnailPath, 'thumbs/' . $recordData[$this->primaryKeyFieldName] . '.' . $ext);
             }
             $updateArray = [];
             if ($Files->getSettings('file_naming') == 'file_id') {
                 $Files->rename($recordData['name'], str_pad($recordData[$this->primaryKeyFieldName], 11, '0', STR_PAD_LEFT) . '.' . $ext);
                 $updateArray['name'] = str_pad($recordData[$this->primaryKeyFieldName], 11, '0', STR_PAD_LEFT) . '.' . $ext;
                 $recordData['name'] = $updateArray['name'];
             }
             if (!empty($updateArray)) {
                 $Update = new Update($tableName);
                 $Update->set($updateArray);
                 $Update->where([$TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]]);
                 $TableGateway->updateWith($Update);
             }
         }
         $this->emitter->run('postInsert', [$TableGateway, $recordData, $this->adapter, $this->acl]);
     }
     $columns = TableSchema::getAllNonAliasTableColumnNames($tableName);
     $recordData = $TableGateway->fetchAll(function ($select) use($recordData, $columns, $TableGateway) {
         $select->columns($columns)->limit(1);
         $select->where->equalTo($TableGateway->primaryKeyFieldName, $recordData[$TableGateway->primaryKeyFieldName]);
     })->current();
     return $recordData;
 }