### Install Upscheme Package Source: https://github.com/aimeos/upscheme/blob/master/README.md Install the Upscheme package using Composer. ```bash composer req aimeos/upscheme ``` -------------------------------- ### Get Sequence Initial Value Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the initial value of a sequence using the __get() magic method or the start() method. ```php $value = $sequence->getInitialValue(); // same as $value = $sequence->start(); ``` -------------------------------- ### Sequence::start() Source: https://github.com/aimeos/upscheme/blob/master/README.md Sets or retrieves the starting value of the sequence. ```APIDOC ## Sequence::start() ### Description Sets the new start value of the sequence or returns the current value. ### Method `public function start( int $value = null )` ### Parameters - **value** (int) - Optional - The new start value for the sequence. If NULL, the current value is returned. ### Response - **self|int** - Returns the same object for setting the value, or the current integer value if no parameter is provided. ### Request Example ```php $value = $sequence->start(); $sequence->start( 1000 ); ``` ``` -------------------------------- ### Get Sequence Properties Source: https://github.com/aimeos/upscheme/blob/master/README.md Access properties like cache, start, and step within a sequence configuration callback to retrieve information about the sequence's current state. ```php $this->db()->sequence( 'seq_test', function( $seq ) { // returns how many generated numbers are cached $cache = $seq->cache; // returns the number the sequence has started from $start = $seq->start; // returns the step width for newly generated numbers $step = $seq->step; } ); ``` -------------------------------- ### Create a Database Sequence Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the sequence() method to create a new database sequence. You can optionally provide a callback to configure its start and step. ```php $this->db()->sequence( 'seq_test' ); ``` ```php $this->db()->sequence( 'seq_test', function( $seq ) { $seq->start( 1000 )->step( 2 ); } ); ``` -------------------------------- ### Create or update a sequence with configuration Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates or updates a sequence with custom configurations like start value, step, and cache size. Call up() to persist changes to the database. ```php public function sequence( string $name, closure $fcn = null ) : Sequence ``` ```php $sequence = $db->sequence( 'seq_test', function( $seq ) { $seq->start( 1000 )->step( 2 )->cache( 100 ); } )->up(); ``` -------------------------------- ### Set Sequence Initial Value Source: https://github.com/aimeos/upscheme/blob/master/README.md Set the initial value of a sequence using the __set() magic method or the start() method. ```php $value = $sequence->setInitialValue( 1000 ); // same as $value = $sequence->start( 1000 ); ``` -------------------------------- ### Doctrine DBAL Schema Creation Example Source: https://github.com/aimeos/upscheme/blob/master/README.md This code snippet demonstrates the verbose nature of creating and modifying a database table using Doctrine DBAL directly. It involves multiple steps for table creation, column definition, and index setting, including platform-specific logic for MySQL/MariaDB. ```php $dbalManager = $conn->createSchemaManager(); $from = $manager->createSchema(); $to = $manager->createSchema(); if( $to->hasTable( 'test' ) ) { $table = $to->getTable( 'test' ); } else { $table = $to->createTable( 'test' ); } $table->addOption( 'engine', 'InnoDB' ); $table->addColumn( 'id', 'integer', ['autoincrement' => true] ); $table->addColumn( 'domain', 'string', ['length' => 32] ); $platform = $conn->getDatabasePlatform(); if( $platform instanceof \Doctrine\DBAL\Platform\MySQLPlatform || $platform instanceof \Doctrine\DBAL\Platform\MariaDBPlatform ) { $table->addColumn( 'code', 'string', ['length' => 64, 'customSchemaOptions' => ['charset' => 'binary']] ); } else { $table->addColumn( 'code', 'string', ['length' => 64]] ); } $table->addColumn( 'label', 'string', ['length' => 255] ); $table->addColumn( 'pos', 'integer', ['default' => 0] ); $table->addColumn( 'status', 'smallint', [] ); $table->addColumn( 'mtime', 'datetime', [] ); $table->addColumn( 'ctime', 'datetime', [] ); $table->addColumn( 'editor', 'string', ['length' => 255] ); $table->setPrimaryKey( ['id'] ); $table->addUniqueIndex( ['domain', 'code'] ); $table->addIndex( ['status', 'pos'] ); foreach( $from->getMigrateToSql( $to, $conn->getDatabasePlatform() ) as $sql ) { $conn->executeStatement( $sql ); } ``` -------------------------------- ### Retrieve Table Options Source: https://github.com/aimeos/upscheme/blob/master/README.md Access table properties like engine, temporary status, charset, and collation to get their current values. ```php $this->db()->table( 'test', function( $table ) { // return the used table engine (only MySQL, MariaDB, etc.) $engine = $table->engine; // returns TRUE if it's a temporary table $isTemp = $table->temporary; // return the current charset $charset = $table->charset; // return the current collation $collation = $table->collation; } ); ``` -------------------------------- ### Create or Get Column Index Source: https://github.com/aimeos/upscheme/blob/master/README.md This method is used to create a regular index for a column. You can optionally provide a name for the index; otherwise, it will be generated automatically. ```php $column->index(); $column->index( 'idx_col' ); ``` -------------------------------- ### Create GUID Column in Table Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new column of type 'guid' or returns the existing one if it already exists. The column is created if it doesn't exist. ```php $table->guid( 'testcol' ); ``` -------------------------------- ### Get Database Name Source: https://github.com/aimeos/upscheme/blob/master/README.md Returns the name of the currently connected database. ```php public function name() : string ``` ```php $db->name(); ``` -------------------------------- ### Get Table Name Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieves the name of the table. ```php public function name() : string ``` ```php $tablename = $table->name(); ``` -------------------------------- ### Get a sequence object Source: https://github.com/aimeos/upscheme/blob/master/README.md Returns the sequence object for a given name. If the sequence does not exist, it will be created. ```php public function sequence( string $name, closure $fcn = null ) : Sequence ``` ```php $sequence = $db->sequence( 'seq_test' ); ``` -------------------------------- ### Build and Execute SQL Delete Statement Source: https://github.com/aimeos/upscheme/blob/master/README.md Use DB::stmt() to get a query builder for constructing and executing SQL DELETE statements. Remember to quote all table and column names using $db->qi(). ```php $db->stmt()->delete( $db->qi( 'test' ) ) ->where( $db->qi( 'stat' ) . ' = ?' )->setParameter( 0, false ) ->executeStatement(); ``` -------------------------------- ### Get Sequence Name Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the name of the sequence using the name() method. ```php $name = $sequence->name(); ``` -------------------------------- ### Define and Modify Table Schema Source: https://github.com/aimeos/upscheme/blob/master/README.md Example of defining a table with various column types, constraints, and indexes using Upscheme's fluent API. This snippet demonstrates creating a 'test' table with an auto-incrementing ID, string columns, integer columns, unique constraints, and indexes. ```php $this->db()->table( 'test', function( $t ) { $t->engine = 'InnoDB'; $t->id(); $t->string( 'domain', 32 ); $t->string( 'code', 64 )->opt( 'charset', 'binary', ['mariadb', 'mysql'] ); $t->string( 'label', 255 ); $t->int( 'pos' )->default( 0 ); $t->smallint( 'status' ); $t->default(); $t->unique( ['domain', 'code'] ); $t->index( ['status', 'pos'] ); } ); ``` -------------------------------- ### Register Custom Sequence Method Source: https://github.com/aimeos/upscheme/blob/master/README.md Register custom methods using the macro() static method. These methods can access class properties like $this->start() and $this->step(). ```php \Aimeos\Upscheme\Schema\Sequence::macro( 'default', function() { $this->start( 1 )->step( 2 ); } ); $sequence->default(); ``` -------------------------------- ### Table::guid() Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new column of type "guid" or returns the existing one. If the column doesn't exist yet, it will be created. ```APIDOC ## Table::guid() ### Description Creates a new column of type "guid" or returns the existing one. If the column doesn't exist yet, it will be created. ### Method `guid( string $name )` ### Parameters #### Path Parameters - **name** (string) - Required - Name of the column ### Response - ** \Aimeos\Upscheme\Schema\Column** - Column object ### Request Example ```php $table->guid( 'testcol' ); ``` ``` -------------------------------- ### Get or Set Foreign Key onDelete Action Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the onDelete() method to either retrieve the current delete action or set a new one. Supports fluid interface calls when setting. ```php public function onDelete( string $value = null ) ``` ```php $value = $foreign->onDelete(); ``` ```php $foreign->onDelete( 'SET NULL' ); ``` ```php $foreign->onDelete( 'SET NULL' )->onUpdate( 'SET NULL' ); ``` -------------------------------- ### Set Custom Schema Options for a Column Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the opt() method to set custom schema options for a column. This example sets the collation for a 'string' column named 'code' to 'utf8mb4'. ```php $this->db()->table( 'test', function( $table ) { $table->string( 'code' )->opt( 'collation', 'utf8mb4' ); } ); ``` -------------------------------- ### Get or Set Column Type Source: https://github.com/aimeos/upscheme/blob/master/README.md Use type() to retrieve the current column type or set a new one. For example, set the type to 'tinyint'. ```php public function type( string $value = null ) ``` ```php $value = $column->type(); $column->type( 'tinyint' ); ``` -------------------------------- ### Get or Set Column Option Source: https://github.com/aimeos/upscheme/blob/master/README.md This method is used to set or retrieve specific options for a column. You can specify an option name, an optional value, and optionally the database type it applies to. ```php $value = $column->opt( 'length' ); $column->opt( 'length', 64 ); ``` -------------------------------- ### Basic Database Configuration and Migration Execution Source: https://github.com/aimeos/upscheme/blob/master/README.md Use this snippet to execute migration tasks with a basic MySQL configuration. Ensure the database credentials and path to migrations are correctly set. ```php $config = [ 'driver' => 'pdo_mysql', 'host' => '127.0.0.1', 'dbname' => '', 'user' => '', 'password' => '' ]; \Aimeos\Upscheme\Up::use( $config, 'src/migrations' )->up(); ``` -------------------------------- ### Create Primary, Unique, and Spatial Indexes Source: https://github.com/aimeos/upscheme/blob/master/README.md Demonstrates creating primary, unique, and spatial indexes on single columns, with options for custom names where applicable. Note that custom primary key names might be ignored by some database systems. ```php $this->db()->table( 'test', function( $table ) { // primary key $table->int( 'id' )->primary(); $table->int( 'id' )->primary( 'pk_test_id' ); // ignored by MySQL, MariaDB, etc. // unique key $table->string( 'code' )->unique(); $table->string( 'code' )->unique( 'unq_test_code' ); // spatial index $table->col( 'location', 'point' )->spatial(); $table->col( 'location', 'point' )->spatial( 'idx_test_location' ); } ); ``` -------------------------------- ### Get or Set Autoincrement Flag Source: https://github.com/aimeos/upscheme/blob/master/README.md Use seq() to get the current autoincrement value or set it. Pass true to enable autoincrement. ```php public function seq( bool $value = null ) ``` ```php $value = $column->seq(); $column->seq( true ); ``` -------------------------------- ### Specify Migration Dependencies Source: https://github.com/aimeos/upscheme/blob/master/README.md Use `after()` and `before()` methods to define execution order for migration tasks. Task names correspond to file names without the .php extension. ```php return new class( $this ) extends Base { public function after() : array { return ['CreateRefTable']; } public function before() : array { return ['InsertTestData']; } } ``` -------------------------------- ### Get or Set Foreign Key onUpdate Action Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the onUpdate() method to either retrieve the current update action or set a new one. Supports fluid interface calls when setting. ```php public function onUpdate( string $value = null ) ``` ```php $value = $foreign->onUpdate(); ``` ```php $foreign->onUpdate( 'SET NULL' ); ``` ```php $foreign->onUpdate( 'SET NULL' )->onDelete( 'SET NULL' ); ``` -------------------------------- ### Get or Set Unsigned Flag Source: https://github.com/aimeos/upscheme/blob/master/README.md Use unsigned() to get the current unsigned flag or set it. Pass true to enable the unsigned property. ```php public function unsigned( bool $value = null ) ``` ```php $value = $column->unsigned(); $column->unsigned( true ); ``` -------------------------------- ### Get or Set Autoincrement Property Source: https://github.com/aimeos/upscheme/blob/master/README.md The `autoincrement()` method can be used to get the current autoincrement status of a column or to set it. It acts as an alias for the `seq()` method. ```php $value = $column->autoincrement(); $column->autoincrement( true ); ``` -------------------------------- ### Quoting Values for SQL Statements Source: https://github.com/aimeos/upscheme/blob/master/README.md Explains how to use the q() method to safely quote values when embedding them directly into SQL statements, recommending prepared statements for security. ```php $db = $this->db(); $result = $db->stmt()->select( '*' )->from( 'products' ) ->where( 'status = ' . $db->q( $_GET['status'] ) )->executeQuery(); ``` -------------------------------- ### Multiple Migration Paths Source: https://github.com/aimeos/upscheme/blob/master/README.md Specify multiple directories for migration tasks by passing an array of paths to the Up::use() method. ```php \Aimeos\Upscheme\Up::use( $config, ['src/migrations', 'ext/migrations'] )->up(); ``` -------------------------------- ### Create uuid column Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new column of type 'guid' or returns the existing one. This method is an alias for guid(). If the column doesn't exist, it will be created. ```php $table->uuid( 'testcol' ); ``` -------------------------------- ### Get or Set Column Default Value Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the default value of a column or specify a new default. Call with no parameters to get the current default, or pass a value to set it. ```php $value = $column->default(); $column->default( 0 ); ``` -------------------------------- ### Generate Migrations from Database Source: https://github.com/aimeos/upscheme/blob/master/README.md Automatically generate migration files for sequences, tables, and views using `Up::use()->create()`. Pass an array of connection keys to `create()` to generate migrations for multiple databases. ```php $config = [ 'db' => [ 'driver' => 'pdo_mysql', 'host' => '127.0.0.1', 'dbname' => '', 'user' => '', 'password' => '' ] ]; \Aimeos\Upscheme\Up::use( $config, 'migrations' )->create(); ``` ```php $config = [ 'db' => [ 'driver' => 'pdo_mysql', // ... ], 'order' => [ 'driver' => 'pdo_oci', // ... ] ]; \Aimeos\Upscheme\Up::use( $config, 'migrations' )->create( ['db', 'order'] ); ``` -------------------------------- ### Get or Set Column Comment Source: https://github.com/aimeos/upscheme/blob/master/README.md This method allows you to get the existing comment for a column or set a new one. Provide a string to update the comment, or call it with no arguments to retrieve it. ```php $comment = $column->comment(); $column->comment( 'column comment' ); ``` -------------------------------- ### Use Custom Table Methods Source: https://github.com/aimeos/upscheme/blob/master/README.md Demonstrates using custom 'defaults' and 'utinyint' methods when defining a table schema. This streamlines table creation. ```php $this->db()->table( 'test', function( $table ) { $table->defaults(); $table->utinyint( 'status' ); } ); ``` -------------------------------- ### Shortcut for Setting OnDelete and OnUpdate Source: https://github.com/aimeos/upscheme/blob/master/README.md The do() method provides a shortcut to set both onDelete and onUpdate actions to the same value, e.g., 'SET NULL'. ```php $this->db()->table( 'users_address', function( $table ) { $table->foreign( 'parentid', 'users' )->do( 'SET NULL' ); } ); ``` -------------------------------- ### Get or Set Column Scale Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the scale of a column or set a new scale value. Pass an integer to set the scale, or call without arguments to get the current scale. ```php $value = $column->scale(); $column->scale( 3 ); ``` -------------------------------- ### Get or Set Column Precision Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the precision of a column or set a new precision value. Pass an integer to set the precision, or call without arguments to get the current precision. ```php $value = $column->precision(); $column->precision( 10 ); ``` -------------------------------- ### Get or Set Column Length Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the defined length for a column or set a new length. Pass an integer to set the length, or call without parameters to get the current length. ```php $value = $column->length(); $column->length( 32 ); ``` -------------------------------- ### Get or Set Column Collation Source: https://github.com/aimeos/upscheme/blob/master/README.md Use this method to retrieve the current collation of a column or set a new one. Pass a string value to set, or call without arguments to get. ```php $comment = $column->collation(); $column->collation( 'binary' ); ``` -------------------------------- ### Database::sequence() Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new database sequence or configures an existing one. ```APIDOC ## Database::sequence() ### Description Creates a new database sequence or configures an existing one. ### Method sequence ### Parameters #### Path Parameters - **name** (string) - Required - Name of the sequence - **callback** (callable) - Optional - Callback function to configure sequence properties (start, step, cache) ### Request Example ```php $this->db()->sequence( 'seq_test', function( $seq ) { $seq->start( 1000 )->step( 2 ); } ); ``` ``` -------------------------------- ### Foreign::up() Source: https://github.com/aimeos/upscheme/blob/master/README.md Applies the changes to the database schema. ```APIDOC ## Foreign::up() ### Description Applies the changes to the database schema. ### Method up ### Response #### Success Response - **self** (self) - Same object for fluid method calls ### Request Example ```php $foreign->up(); ``` ``` -------------------------------- ### Get Column Name Source: https://github.com/aimeos/upscheme/blob/master/README.md This method returns the name of the column. ```php $name = $column->name(); ``` -------------------------------- ### Set Database-Specific Table Options Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the 'opt()' method with a third parameter to specify database server types for charset and collation options. ```php $this->db()->table( 'test', function( $table ) { $table->opt( 'charset', 'utf8mb4', 'mysql' ); $table->opt( 'collation', 'utf8mb4_unicode_ci', 'mysql' ); } ); ``` -------------------------------- ### Set Foreign Key Option via opt() Method Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the opt() method to set foreign key options, providing the option name and value as arguments. This is another way to configure these settings. ```php $foreign->opt( 'onDelete', 'SET NULL' ); ``` -------------------------------- ### Create a Database View Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a database view with a given name and SQL statement if it does not already exist. An optional database type can be specified. ```php public function view( string $name, string $sql, $for = null ) : self ``` ```php $db->view( 'testview', 'SELECT * FROM testtable' ); ``` ```php $db->view( 'testview', 'SELECT id, label, status FROM testtable WHERE status = 1' ); ``` ```php $db->view( 'testview', 'SELECT * FROM `testtable` WHERE `status` = 1', 'mysql' ); ``` -------------------------------- ### Sequence::up() Source: https://github.com/aimeos/upscheme/blob/master/README.md Applies any pending changes to the database schema. ```APIDOC ## Sequence::up() ### Description Applies the changes to the database schema. ### Method `public function up() : self` ### Response - **self** - Returns the same object for fluid method calls. ### Request Example ```php $sequence->up(); ``` ``` -------------------------------- ### Quoting Reserved Keywords in SQL Source: https://github.com/aimeos/upscheme/blob/master/README.md Demonstrates using the qi() method to quote schema elements (like column names) that are reserved keywords in SQL. ```php $db = $this->db(); $result = $db->stmt()->select( $db->qi( 'key' ) )->from( 'products' )->executeQuery(); ``` -------------------------------- ### Get Sequence Step Value Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the current step value of the sequence by calling step() without parameters. ```php $value = $sequence->step(); ``` -------------------------------- ### Registering Custom Connection Macro Source: https://github.com/aimeos/upscheme/blob/master/README.md Register a custom macro to transform a non-DBAL configuration into valid DBAL settings for connecting to the database. ```php \Aimeos\Upscheme\Up::macro( 'connect', function( array $cfg ) { return \Doctrine\DBAL\DriverManager::getConnection( [ 'driver' => $cfg['adapter'], 'host' => $cfg['host'], 'dbname' => $cfg['database'], 'user' => $cfg['username'], 'password' => $cfg['password'] ] ); } ); ``` -------------------------------- ### Get Sequence Cache Size Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the current cached size of the sequence by calling cache() without parameters. ```php $value = $sequence->cache(); ``` -------------------------------- ### DB::up() Source: https://github.com/aimeos/upscheme/blob/master/README.md Applies all pending schema changes to the database. ```APIDOC ## DB::up() ### Description Applies the changes to the database schema that have been defined. ### Method ```php public function up() : self ``` ### Returns * **self** Same object for fluid method calls ### Examples ```php $db->up(); ``` ``` -------------------------------- ### Enabling Verbose Output for Migrations Source: https://github.com/aimeos/upscheme/blob/master/README.md Enable verbose output for migration tasks using the verbose() method with different levels of verbosity. ```php \Aimeos\Upscheme\Up::use( $config, 'src/migrations' )->verbose()->up(); // most important only ``` ```php \Aimeos\Upscheme\Up::use( $config, 'src/migrations' )->verbose( 'vv' )->up(); // more verbose ``` ```php \Aimeos\Upscheme\Up::use( $config, 'src/migrations' )->verbose( 'vvv' )->up(); // debugging ``` -------------------------------- ### Output Messages in Migration Tasks Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the `info()` method to output messages during migration. The second parameter controls verbosity level (v, vv, vvv), and the third parameter indents messages. ```php $this->info( 'some message' ); $this->info( 'more verbose message', 'vv' ); $this->info( 'very verbose debug message', 'vvv' ); ``` ```php $this->info( 'some message' ); $this->info( 'second level message', 'v', 1 ); $this->info( 'third level message', 'v', 2 ); ``` -------------------------------- ### Executing Custom SQL Queries Source: https://github.com/aimeos/upscheme/blob/master/README.md Shows how to execute custom SQL SELECT statements using the query() method, which returns an iterable result set. ```php $sql = 'SELECT id, label, status FROM product WHERE label LIKE ?'; $result = $this->db()->query( $sql, ['test%'] ); foreach( $result->iterateAssociative() as $row ) { // ... } ``` -------------------------------- ### Get Foreign Key Constraint Name Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the name of the foreign key constraint using the name() method. Returns null if no name is set. ```php public function name() ``` ```php $fkname = $foreign->name(); ``` -------------------------------- ### Build and Execute SQL Select Statement Source: https://github.com/aimeos/upscheme/blob/master/README.md Use DB::stmt() to create a query builder for SQL SELECT statements. Table and column names must be quoted using $db->qi(). Fetches results using fetchAssociative(). ```php $result = $db->stmt()->select( $db->qi( 'id' ), $db->qi( 'code' ) ) ->from( $db->qi( 'test' ) ) ->where( $db->qi( 'stat' ) . ' = 1' ) ->executeQuery(); while( $row = $result->fetchAssociative() ) { $id = $row['id']; } ``` -------------------------------- ### Create Custom Table Default Methods Source: https://github.com/aimeos/upscheme/blob/master/README.md Define a 'defaults' method for tables to add common columns like id, ctime, mtime, and editor. This promotes code reuse. ```php \Aimeos\Upscheme\Schema\Table::marco( 'defaults', function() { $this->id(); $this->datetime( 'ctime' ); $this->datetime( 'mtime' ); $this->string( 'editor' ); return $this; } ); ``` -------------------------------- ### Creating a Table with Named Class Migration Source: https://github.com/aimeos/upscheme/blob/master/README.md Define a migration task using a named class to create a new table with specified columns. Ensure the file name matches the class name. ```php db()->table( 'test', function( Table $t ) { $t->id(); $t->string( 'label' ); $t->bool( 'status' ); } ); } } ``` -------------------------------- ### Retrieve Database Schema as Array Source: https://github.com/aimeos/upscheme/blob/master/README.md Use DB::toArray() to get a representation of the current database schema, including sequences, tables, and views, as an associative array. ```php $this->db()->toArray(); ``` -------------------------------- ### Access Table Options via Magic Getter Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve table-specific options like 'engine' using the magic __get() method. This provides a convenient way to access configuration values. ```php $engine = $table->engine; // same as $engine = $table->opt( 'engine' ); ``` -------------------------------- ### Get Table Object Without Definition Source: https://github.com/aimeos/upscheme/blob/master/README.md Use DB::table() without a closure to retrieve an existing table object. This is useful for inspecting table definitions. ```php $table = $db->table( 'test' ); ``` -------------------------------- ### Set Table Engine Option Source: https://github.com/aimeos/upscheme/blob/master/README.md Specify the storage engine for a table. Use the 'opt()' method for explicit option setting. ```php $this->db()->table( 'test', function( $table ) { $table->opt( 'engine', 'InnoDB' ); } ); ``` -------------------------------- ### Create or Get Column Primary Index Source: https://github.com/aimeos/upscheme/blob/master/README.md This method is used to create a primary index for a column. You can optionally provide a name for the primary key; otherwise, it will be generated automatically. ```php $column->primary(); $column->primary( 'pk_col' ); ``` -------------------------------- ### Get Last Inserted ID Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieves the ID of the most recently inserted row across any table. Note: Does not work for Oracle platform due to Doctrine DBAL limitations. ```php public function lastId() : string ``` ```php $db->lastId(); ``` -------------------------------- ### SQLite Database Configuration Source: https://github.com/aimeos/upscheme/blob/master/README.md Configure Upscheme for SQLite databases by specifying the driver and the path to the SQLite database file. ```php $config = [ 'driver' => 'pdo_sqlite', 'path' => 'path/to/file.sq3' ]; ``` -------------------------------- ### Add Database Specific Column Type Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the col() method to add database-specific column types. This example adds a 'tinyint' status column to the 'test' table. ```php $this->db()->table( 'test', function( $table ) { $table->col( 'status', 'tinyint' ); } ); ``` -------------------------------- ### Enable Verbose Output Source: https://github.com/aimeos/upscheme/blob/master/README.md Call the `verbose()` method on the Up class instance before calling `up()` to enable verbose message output. ```php \Aimeos\Upscheme\Up::use( $config, '...' )->verbose()->up(); ``` -------------------------------- ### Get or Set Column Charset Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the `charset()` method to retrieve the current charset of a column or to set a new one. This method returns the object for chaining when setting the value. ```php $comment = $column->charset(); $column->charset( 'utf8' ); ``` -------------------------------- ### Create Temporary Table Source: https://github.com/aimeos/upscheme/blob/master/README.md Mark a table as temporary by setting the 'temporary' property to true. ```php $this->db()->table( 'test', function( $table ) { $table->temporary = true; } ); ``` -------------------------------- ### Multiple Database Connections Configuration Source: https://github.com/aimeos/upscheme/blob/master/README.md Configure Upscheme to use multiple database connections by defining them in a configuration array with distinct keys. ```php $config = [ 'db' => [ 'driver' => 'pdo_mysql', 'host' => '127.0.0.1', 'dbname' => '', 'user' => '', 'password' => '' ], 'temp' => [ 'driver' => 'pdo_sqlite', 'path' => '/tmp/mydb.sqlite' ] ]; \Aimeos\Upscheme\Up::use( $config, 'src/migrations' )->up(); ``` -------------------------------- ### Access Database Sequence Object Source: https://github.com/aimeos/upscheme/blob/master/README.md Get a sequence object using `$this->db()->sequence('sequence_name')`. If the sequence doesn't exist, it will be created; otherwise, the existing object is returned. ```php $seq = $this->db()->sequence( 'seq_users' ); ``` -------------------------------- ### Access Database Table Object Source: https://github.com/aimeos/upscheme/blob/master/README.md Get a table object using `$this->db()->table('table_name')`. If the table doesn't exist, it will be created; otherwise, the existing object is returned. ```php $table = $this->db()->table( 'users' ); ``` -------------------------------- ### Executing Platform-Specific SQL Statements Source: https://github.com/aimeos/upscheme/blob/master/README.md Illustrates using the for() method to execute SQL statements that are specific to a particular database platform, such as creating indexes. ```php $this->db()->for( 'mysql', 'CREATE FULLTEXT INDEX idx_text ON product (text)' ); ``` -------------------------------- ### Set or Get Custom Table Option Source: https://github.com/aimeos/upscheme/blob/master/README.md Sets a custom schema option for the table or retrieves the current value of an option. Available options include charset, collation, engine, and temporary. ```php public function opt( string $name, $value = null ) ``` ```php $charset = $table->opt( 'charset' ); ``` ```php $table->opt( 'charset', 'utf8' )->opt( 'collation', 'utf8_bin' ); ``` ```php $charset = $table->charset; ``` ```php $table->charset = 'binary'; ``` -------------------------------- ### Creating a Table with Anonymous Class Migration Source: https://github.com/aimeos/upscheme/blob/master/README.md Define a migration task using an anonymous class to create a new table with specified columns. ```php db()->table( 'test', function( Table $t ) { $t->id(); $t->string( 'label' ); $t->bool( 'status' ); } ); } }; ``` -------------------------------- ### Table::bigid() Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new ID column of type "bigint" or returns the existing one. The column automatically gets a sequence (autoincrement) and a primary key assigned. ```APIDOC ## Table::bigid() ### Description Creates a new ID column of type "bigint" or returns the existing one. The column gets a sequence (autoincrement) and a primary key assigned automatically. If the column doesn't exist yet, it will be created. ### Method bigid( string $name = null ) : Column ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```php $table->bigid(); $table->bigid( 'uid' ); ``` ### Response #### Success Response (200) - **\Aimeos\Upscheme\Schema\Column**: Column object #### Response Example ```json // No specific JSON example, returns a Column object ``` ``` -------------------------------- ### Attempt to Drop Non-Existent Table Source: https://github.com/aimeos/upscheme/blob/master/README.md Shows that calling 'dropTable()' on a table that does not exist will succeed without error and perform no action. ```php $this->db()->dropTable( 'notexist' ); ``` -------------------------------- ### Get Database Type Source: https://github.com/aimeos/upscheme/blob/master/README.md Use DB::type() to determine the type of the connected database. Possible return values include 'db2', 'mariadb', 'mysql', 'oracle', 'postgresql', 'sqlite', and 'sqlserver'. ```php $type = $db->type(); ``` -------------------------------- ### Use Two Database Connections Simultaneously Source: https://github.com/aimeos/upscheme/blob/master/README.md Obtain a second database schema object with a new connection by passing `true` as the second parameter to `db()`. Remember to call `close()` on the new connection when done. ```php $db1 = $this->db(); $db2 = $this->db( 'db', true ); foreach( $db1->select( 'users', ['status' => false] ) as $row ) { $db2->insert( 'oldusers', $row ); } $db2->delete( 'users', ['status' => false] ); ``` ```php $db2->close(); ``` -------------------------------- ### Build and Execute SQL Update Statement Source: https://github.com/aimeos/upscheme/blob/master/README.md Use DB::stmt() to construct and execute SQL UPDATE statements. Ensure all table and column names are quoted with $db->qi(). ```php $db->stmt()->update( $db->qi( 'test' ) ) ->where( $db->qi( 'stat' ) . '', '?' )->setParameter( 0, true ) ->executeStatement(); ``` -------------------------------- ### Create Binary Column Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new binary column or returns an existing one. Specify the name and optionally the length in bytes. ```php $table->binary( 'testcol' ); $table->binary( 'testcol', 32 ); ``` -------------------------------- ### Get or Set Column Nullability Source: https://github.com/aimeos/upscheme/blob/master/README.md Determine if a column allows NULL values or set its nullability. Pass a boolean to set the flag, or call without arguments to retrieve the current null status. ```php $value = $column->null(); $column->null( true ); ``` -------------------------------- ### Apply Schema Changes to Database Source: https://github.com/aimeos/upscheme/blob/master/README.md Use DB::up() to apply all pending schema changes defined using methods like DB::table() to the actual database. ```php $db->up(); ``` -------------------------------- ### Get or Set Column Fixed Flag Source: https://github.com/aimeos/upscheme/blob/master/README.md Check if a column is fixed or set its fixed status. Pass a boolean to set the flag, or call without arguments to retrieve the current fixed status. ```php $value = $column->fixed(); $column->fixed( true ); ``` -------------------------------- ### Register Custom DB Methods with __call() Source: https://github.com/aimeos/upscheme/blob/master/README.md Allows extending the DB object with custom methods by registering them using the macro() static method. These custom methods can access internal properties like $this->from, $this->to, and $this->conn. ```php public function __call( string $method, array $args ) ``` ```php \Aimeos\Upscheme\Schema\DB::macro( 'hasFkIndexes', function( $val ) { return $this->to->hasExplicitForeignKeyIndexes(); } ); $db->hasFkIndexes(); // returns true/false ``` ```php $db->hasExplicitForeignKeyIndexes(); ``` -------------------------------- ### Apply Multiple Column Modifiers Source: https://github.com/aimeos/upscheme/blob/master/README.md Chain multiple column modifier methods to customize a column definition. This example defines an 'int' column named 'number' that allows null values and is unsigned. ```php $this->db()->table( 'test', function( $table ) { $table->int( 'number' )->null( true )->unsigned( true ); } ); ``` -------------------------------- ### Persist Schema Changes Immediately Source: https://github.com/aimeos/upscheme/blob/master/README.md Call `$this->db()->up()` to immediately apply schema changes to the database, useful before inserting data. ```php $this->db()->up(); ``` -------------------------------- ### Get Column Option Value Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve the value of a specific column option using magic getter syntax or the `opt()` method. This is useful for inspecting column properties like charset or unique constraints. ```php $charset = $column->charset; // same as $charset = $column->opt( 'charset' ); ``` -------------------------------- ### Executing Custom SQL Statements (Non-Query) Source: https://github.com/aimeos/upscheme/blob/master/README.md Demonstrates executing custom SQL statements like UPDATE or INSERT using the exec() method, which returns the number of affected rows. ```php $sql = 'UPDATE product SET status=? WHERE status=?'; $num = $this->db()->exec( $sql, [1, 0] ); ``` -------------------------------- ### Create spatial index Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new spatial index or replaces an existing one. The index name should not exceed 30 characters for compatibility. ```php $table->spatial( 'testcol' ); ``` ```php $table->spatial( ['testcol', 'testcol2'] ); ``` ```php $table->spatial( 'testcol', 'idx_test_testcol' ); ``` -------------------------------- ### Table::id() Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new ID column of type "integer" or returns the existing one. The column gets a sequence (autoincrement) and a primary key assigned automatically. If the column doesn't exist yet, it will be created. ```APIDOC ## Table::id() ### Description Creates a new ID column of type "integer" or returns the existing one. The column gets a sequence (autoincrement) and a primary key assigned automatically. If the column doesn't exist yet, it will be created. ### Method `id( string $name = null )` ### Parameters #### Path Parameters - **name** (string|null) - Optional - Name of the ID column ### Response - ** \Aimeos\Upscheme\Schema\Column** - Column object ### Request Example ```php $table->id(); $table->id( 'uid' ); ``` ``` -------------------------------- ### Advanced Select Query with QueryBuilder Source: https://github.com/aimeos/upscheme/blob/master/README.md Illustrates building a more complex select query using the Doctrine QueryBuilder, including WHERE clauses and parameter binding. ```php $db = $this->db(); $result = $db->stmt()->select( 'id', 'name' ) ->from( 'users' ) ->where( 'status != ?' ) ->setParameter( 0, false ) ->executeQuery(); ``` -------------------------------- ### Set Database-Specific Column Options Source: https://github.com/aimeos/upscheme/blob/master/README.md Specify the database type as the third parameter in the opt() method to set column modifiers for a particular database implementation. This example sets the collation for a 'string' column named 'code' to 'utf8mb4' specifically for MySQL. ```php $this->db()->table( 'test', function( $table ) { $table->string( 'code' )->opt( 'collation', 'utf8mb4', 'mysql' ); } ); ``` -------------------------------- ### Perform Action on Foreign Key Constraint Source: https://github.com/aimeos/upscheme/blob/master/README.md Use the do() method to specify an action to be performed on the foreign key constraint. This method supports fluid interface calls. ```php public function do( string $action ) : self ``` ```php $foreign->do( 'RESTRICT' ); ``` -------------------------------- ### Create Spatial Index Source: https://github.com/aimeos/upscheme/blob/master/README.md Use spatial() to create a spatial index for a column. An optional name can be provided. ```php public function spatial( string $name = null ) : self ``` ```php $column->spatial(); $column->spatial( 'idx_col' ); ``` -------------------------------- ### Create Single Column Index with Custom Name Source: https://github.com/aimeos/upscheme/blob/master/README.md Provide a custom name for the index by passing it as an argument to the `index()` method. Ensure index names are 30 characters or less for maximum compatibility. ```php $this->db()->table( 'test', function( $table ) { $table->string( 'label' )->index( 'idx_test_label' ); } ); ``` -------------------------------- ### Create Basic Foreign Key Constraint Source: https://github.com/aimeos/upscheme/blob/master/README.md Use foreign() to create a foreign key constraint. The first parameter is the referencing column, the second is the referenced table. ```php $this->db()->table( 'users_address', function( $table ) { $table->foreign( 'parentid', 'users' ); } ); ``` -------------------------------- ### Attempt to Rename Non-Existent Table Source: https://github.com/aimeos/upscheme/blob/master/README.md Demonstrates that calling 'renameTable()' on a non-existent table does not report an error and no action is taken. ```php $this->db()->renameTable( 'notexist', 'newtable' ); ``` -------------------------------- ### Foreign Key Macro Registration Source: https://github.com/aimeos/upscheme/blob/master/README.md Allows registering custom methods on the Foreign object, providing access to its internal properties for custom logic. ```APIDOC ## Foreign Key Macro Registration Registers custom methods on the `Upscheme\Schema\Foreign` class. ### Method `macro(string $method, callable $callback)` ### Parameters * **`$method`** (string) - The name of the custom method to register. * **`$callback`** (callable) - A callable function that will be executed when the custom method is called. The callback has access to the Foreign object's properties. ### Available Class Properties within Callback * `$this->dbaltable` - Doctrine table schema. * `$this->table` - Upscheme Table object. * `$this->localcol` - Local column name or names. * `$this->fktable` - Foreign table name. * `$this->fkcol` - Foreign column name or names. * `$this->name` - Foreign key name. * `$this->opts` - Associative list of foreign key options (e.g., 'onDelete', 'onUpdate'). ### Request Example ```php \Aimeos\Upscheme\Schema\Foreign::macro( 'setDefaultOptions', function() { $this->opts = ['onDelete' => 'SET NULL', 'onUpdate' => 'SET NULL']; } ); // Assuming $foreign is an instance of Upscheme\Schema\Foreign $foreign->setDefaultOptions(); ``` ### Response This method does not return a value upon successful registration. The custom method becomes available on Foreign object instances. ``` -------------------------------- ### Add columns with modifiers and index Source: https://github.com/aimeos/upscheme/blob/master/README.md Adds multiple columns to a table with specified types, modifiers, and indexes. Demonstrates adding an 'id' column, a 'string' column with an index, and a 'tinyint' column with a default value. ```php $this->db()->table( 'test', function( $table ) { $table->id()->unsigned( true ); $table->string( 'label' )->index(); $table->col( 'status', 'tinyint' )->default( 0 ); } ); ``` -------------------------------- ### Set Table Engine as Property Source: https://github.com/aimeos/upscheme/blob/master/README.md A shortcut to set the table's storage engine by assigning it directly to the 'engine' property. ```php $this->db()->table( 'test', function( $table ) { $table->engine = 'InnoDB'; } ); ``` -------------------------------- ### Create Datetime Column Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new datetime column or returns an existing one. Specify the column name. ```php $table->datetime( 'testcol' ); ``` -------------------------------- ### DB::name() Source: https://github.com/aimeos/upscheme/blob/master/README.md Returns the name of the database. ```APIDOC ## DB::name() ### Description Returns the name of the database. ### Method Not specified (assumed to be a method call on a DB object). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Return Value * **string** - Database name ### Examples ```php $db->name(); ``` ``` -------------------------------- ### Create Primary Key Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new primary key index or replaces an existing one. The index name should not exceed 30 characters for compatibility. ```php public function primary( $columns, string $name = null ) : self ``` ```php $table->primary( 'testcol' ); ``` ```php $table->primary( ['testcol', 'testcol2'] ); ``` ```php $table->primary( 'testcol', 'pk_test_testcol' ); ``` -------------------------------- ### Access foreign key options Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieve specific foreign key options like `onDelete` or `onUpdate` using magic getter syntax. These options control behavior when related records are modified. ```php $value = $foreign->onDelete; // same as $value = $foreign->opt( 'onDelete' ); ``` -------------------------------- ### Sequence::__get() Source: https://github.com/aimeos/upscheme/blob/master/README.md Retrieves the value of a specified sequence option. ```APIDOC ## Sequence::__get() ### Description Returns the value for the given sequence option. ### Method `public function __get( string $name )` ### Parameters - **name** (string) - Required - The name of the sequence option. ### Response - **mixed** - The value of the sequence option. ### Request Example ```php $value = $sequence->getInitialValue(); // same as $value = $sequence->start(); ``` ``` -------------------------------- ### Table::index() Source: https://github.com/aimeos/upscheme/blob/master/README.md Creates a new index or replaces an existing one on the table. It accepts a single column name or an array of column names, and an optional index name. ```APIDOC ## Table::index() ### Description Creates a new index or replaces an existing one. ### Method Signature `public function index( $columns, string $name = null ) : self` ### Parameters * **columns** (array|string) - Required - Name of the columns or columns spawning the index * **name** (string|null) - Optional - Index name or NULL for autogenerated name ### Returns * **self** - Same object for fluid method calls ### Notes The length of the index name shouldn't be longer than 30 characters for maximum compatibility. ### Examples ```php $table->index( 'testcol' ); $table->index( ['testcol', 'testcol2'] ); $table->index( 'testcol', 'idx_test_testcol ); ``` ``` -------------------------------- ### Oracle Database Configuration Source: https://github.com/aimeos/upscheme/blob/master/README.md Configure Upscheme for Oracle databases, including specific parameters for Oracle 18+. ```php $config = [ 'driver' => 'pdo_oci', 'host' => '', 'dbname' => '', 'service' => true, // for Oracle 18+ only 'user' => '', 'password' => '' ]; ``` -------------------------------- ### Create Table-Level Spatial Index Source: https://github.com/aimeos/upscheme/blob/master/README.md Create a spatial index using the table object's `spatial()` method. Note that spatial indexes cannot span multiple columns. ```php $this->db()->table( 'test', function( $table ) { $table->spatial( 'location' ); } ); ```