Inserts model to database

@param string|null $table    Table name
@param SqlInterface $database Dabase object

@return Model
489279
public function insert($table = null, SqlInterface $database = null)
    {
        //if no table
        if (is_null($table)) {
            //if no default table either
            if (!$this->table) {
                //throw error
                throw SqlException::forTableNotSet();
            }

            $table = $this->table;
        }

        //if no database
        if (is_null($database)) {
            //and no default database
            if (!$this->database) {
                throw SqlException::forDatabaseNotSet();
            }

            $database = $this->database;
        }

        //get the meta data, the valid column values and whether is primary is set
        $meta = $this->getMeta($table, $database);
        $data = $this->getValidColumns(array_keys($meta[self::COLUMNS]));

        //we insert it
        $database->insertRow($table, $data);

        //only if we have 1 primary key
        if (count($meta[self::PRIMARY]) == 1) {
            //set the primary key
            $this->data[$meta[self::PRIMARY][0]] = $database->getLastInsertedId();
        }

        return $this;
    }
1351524
protected function newTable($table = null)
    {
        return new Table(
            static::$dbManager,
            $this->connectionName,
            $table ? : $this->tableName,
            static::$dbManager->getStructure($this->connectionName)
        );
    }
899737
public static function insert($table = null)
    {
        $query = new Insert();

        if ($table) {
            $query->into($table);
        }

        return $query;
    }
102419
public function insert(string $table): Query\Insert
    {
        $query = new Query\Insert();
        $query->insert($table);
        return $query;
    }
803040
public function pass(DataBagInterface $dataBag): DataBagInterface
    {
        $dataToInsert = $this->getDataToInsert($dataBag);
        if ($this->recordExists($dataBag)) {
            $this->updateRecord($dataToInsert, $dataBag);
        } else {
            $this->insertRecord($dataToInsert, $dataBag);
        }

        return $dataBag;
    }
309596
public function insert($table, $data)
    {
        $query = $this->provider->buildInsertQuery($table, $data);

        return $this->provider->query($query, null);
    }
1197817
public function insert($table = "")
    {
        $this->start = "";
        $this->queryType = self::QUERY_INSERT;
        $this->into($table);
        return $this;
    }
953799
public function createModel($persistence, $table = null)
    {
        $m = new \atk4\data\Model([$persistence, 'table'=>$table ?: $this['table'] = $table]);

        foreach ($this->_getFields() as $field => $options) {
            if ($field == 'id') {
                continue;
            }

            if (is_object($options)) {
                continue;
            }

            $defaults = [];

            if ($options['type']) {
                $defaults['type'] = $options['type'];
            }
            $m->addField($field, $defaults);
        }

        return $m;
    }
996199
public function insert(string $table) : QueryBuilder
    {
        $this->_query[] = "INSERT INTO";
        $this->_query[] = "`". $this->sanitize($table) ."`";

        return $this;
    }
1025834
public function db($database = null)
    {
        if (empty($database)) {
            $database = $this->database;
        }
        return $this->silex['dbs'][$database];
    }
309667
public function listTableForeignKeys($table, $database = null)
    {
        if ($database === null) {
            $database = $this->_conn->getDatabase();
        }
        $sql              = $this->_platform->getListTableForeignKeysSQL($table, $database);
        $tableForeignKeys = $this->_conn->fetchAll($sql);

        return $this->_getPortableTableForeignKeysList($tableForeignKeys);
    }
1291203
final public function createTableIfNotExists(Table $table): Database
    {
        if (!$this->hasTable($table::getName())) {
            $this->createTable($table);
        }

        return $this;
    }
100835
public function PrimaryKey($Table, $Database = NULL) {
      $Schema = $this->Fetch($Table, $Database);
      $PrimaryKeys = array();
      foreach ($Schema as $FieldName => $Properties) {
         if ($Properties->PrimaryKey === TRUE)
            $PrimaryKeys[] = $FieldName;
      }
      
      if (count($PrimaryKeys) == 0)
         return '';
      elseif (count($PrimaryKeys) == 1)
         return $PrimaryKeys[0];
      else
         return $PrimaryKeys;
   }
1208998
public function haveInDatabase($table, array $data)
    {
        $lastInsertId = $this->_insertInDatabase($table, $data);

        $this->addInsertedRow($table, $data, $lastInsertId);

        return $lastInsertId;
    }
965540
public function processSql(SqlInterface $sql)
    {
        $this->_sql = $sql;
        $tableName = $sql->getTable();

        $ok = $this->_checkUnsupportedFeatures();
        if (!$ok) {
            throw new ServiceException(
                "SQLite 'ALTER TABLE' only supports the adding new columns."
            );
        }

        $addColumns = $this->_parseColumns();
        if ($addColumns) {
            $this->_statements[] = "ALTER TABLE {$tableName} " .
                "ADD COLUMN {$addColumns}";
        }

        return implode(';', $this->_statements);
    }
581688
public function runInstall(Model $entity, ?string $database): void
    {
        $database = $this->asConnection($entity, $database);
        $table = $this->resolveMigrationTableName($entity);
        $migrator = $this->resolveMigrator($table);
        $repository = $migrator->getRepository();

        $migrator->setConnection($database);

        if (! $repository->repositoryExists()) {
            $repository->createRepository();

            $this->note("<info>Migration table {$table} created successfully.</info>");
        }

        $migrator->resetConnection();
    }
670967
public function database(string $database = null): DatabaseInterface
    {
        if (empty($database)) {
            $database = $this->config->getDefaultDatabase();
        }

        //Spiral support ability to link multiple virtual databases together using aliases
        $database = $this->config->resolveAlias($database);

        if (isset($this->databases[$database])) {
            return $this->databases[$database];
        }

        if (!$this->config->hasDatabase($database)) {
            throw new DBALException(
                "Unable to create Database, no presets for '{$database}' found"
            );
        }

        return $this->databases[$database] = $this->makeDatabase(
            $this->config->getDatabase($database)
        );
    }