<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Arman</title>
    <description>The latest articles on Forem by Arman (@armanist84).</description>
    <link>https://forem.com/armanist84</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F885485%2F2ed37645-369f-4022-bb24-9a96daa73ea4.png</url>
      <title>Forem: Arman</title>
      <link>https://forem.com/armanist84</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/armanist84"/>
    <language>en</language>
    <item>
      <title>What’s New in Quantum PHP Framework 2.9 Release</title>
      <dc:creator>Arman</dc:creator>
      <pubDate>Sat, 11 Jan 2025 10:39:28 +0000</pubDate>
      <link>https://forem.com/armanist84/whats-new-in-quantum-php-framework-29-release-3o1j</link>
      <guid>https://forem.com/armanist84/whats-new-in-quantum-php-framework-29-release-3o1j</guid>
      <description>&lt;p&gt;The Quantum PHP Framework team has wrapped up 2024 with an exciting new release — version 2.9, which debuted at the end of November. This update, arriving just under a year after the 2.8 release, brings a fresh wave of enhancements and features, reinforcing Quantum’s position as a robust and developer-friendly framework.&lt;/p&gt;

&lt;p&gt;Version 2.9 introduces a powerful set of tools designed to simplify and elevate web development. Highlights include a new &lt;strong&gt;CAPTCHA&lt;/strong&gt; library to bolster security, an &lt;strong&gt;Archive&lt;/strong&gt; library for seamless file compression, and significant updates to the &lt;strong&gt;Mailer&lt;/strong&gt; library, enhancing email handling. The &lt;strong&gt;Filesystem&lt;/strong&gt; library now supports a &lt;strong&gt;Google Drive adapter&lt;/strong&gt;, making cloud storage integration effortless. Developers can also look forward to a new &lt;strong&gt;Module generation command&lt;/strong&gt;, streamlining the creation of modular applications, and an all-new &lt;strong&gt;pagination&lt;/strong&gt; feature for improved data navigation.&lt;/p&gt;

&lt;p&gt;Let’s dive into the details of these features and explore how they can benefit your next project!&lt;/p&gt;

&lt;h2&gt;
  
  
  Enhanced Security with the New CAPTCHA Library
&lt;/h2&gt;

&lt;p&gt;The Quantum PHP Framework 2.9 introduces a new CAPTCHA library, empowering developers to integrate advanced security mechanisms into their applications. With support for two of the most popular CAPTCHA services &lt;strong&gt;reCAPTCHA ** and **hCaptcha&lt;/strong&gt;. This library offers robust protection against bots and automated abuse. The library supports both visible CAPTCHAs, where users interact directly with security challenges, and invisible CAPTCHAs, which work silently in the background to verify users without disrupting their experience.&lt;/p&gt;

&lt;p&gt;To begin, add the necessary CAPTCHA keys to your &lt;code&gt;.env&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RECAPTCHA_SITE_KEY=
RECAPTCHA_SECRET_KEY=

HCAPTCHA_SITE_KEY=
HCAPTCHA_SECRET_KEY=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;shared/config/captcha.php&lt;/code&gt; configuration file, specify the desired adapter (reCAPTCHA or hCaptcha) and set its type (visible or invisible). Here's an example configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return [
    /**
     * ---------------------------------------------------------
     * Captcha configurations
     * ---------------------------------------------------------
     */
    'current' =&amp;gt; 'recaptcha', // Set to 'recaptcha' or 'hcaptcha'

    'recaptcha' =&amp;gt; [
        'type' =&amp;gt; 'visible', // Options: 'visible' or 'invisible'
        'site_key' =&amp;gt; env('RECAPTCHA_SITE_KEY'),
        'secret_key' =&amp;gt; env('RECAPTCHA_SECRET_KEY'),
    ],

    'hcaptcha' =&amp;gt; [
        'type' =&amp;gt; 'invisible',
        'site_key' =&amp;gt; env('HCAPTCHA_SITE_KEY'),
        'secret_key' =&amp;gt; env('HCAPTCHA_SECRET_KEY'),
    ],
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your view file (e.g., the signup page), integrate the CAPTCHA widget into your form using the helper function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo captcha()-&amp;gt;addToForm('signUpForm', ['class' =&amp;gt; 'col s1 offset-s4']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$formIdentifier&lt;/strong&gt;: A string parameter required for invisible CAPTCHA. It binds the CAPTCHA to the specified form.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$attributes&lt;/strong&gt;: An array parameter used for visible CAPTCHA, allowing you to define custom tag attributes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To verify the CAPTCHA response, either create new middleware or update an existing one. For example, in the &lt;code&gt;modules/Web/Middlewares/Signup.php&lt;/code&gt; middleware, you can add the validation rule alongside other rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$this-&amp;gt;validator-&amp;gt;addRules([
    // Other validation rules...
    'captcha' =&amp;gt; [
        Rule::set('required'),
        Rule::set('captcha'),
    ],
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Simplify File Compression with the New Archive Library
&lt;/h2&gt;

&lt;p&gt;Quantum PHP Framework 2.9 introduces a robust Archive library, making file compression and extraction more straightforward than ever. This library provides two versatile adapters — &lt;strong&gt;Zip&lt;/strong&gt; and &lt;strong&gt;Phar&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Creating a Zip Archive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Quantum\Libraries\Archive\Adapters\ZipAdapter;


$zip = new ZipAdapter('my-archive.zip');

$zip-&amp;gt;addFile('path/to/file1.txt', 'documents/file1.txt');
$zip-&amp;gt;addFile('path/to/image.jpg', 'images/image.jpg');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Extracting Files from a Zip Archive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Quantum\Libraries\Archive\Adapters\ZipAdapter;

$zip = new ZipAdapter('my-archive.zip');

$zip-&amp;gt;extractTo('path/to/extract/directory');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add multiple files to archive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$zip-&amp;gt;addMultipleFiles([
    ['path' =&amp;gt; 'path/to/source/file1.txt', 'name' =&amp;gt; 'file1.txt'],
    ['path' =&amp;gt; 'path/to/source/file2.txt', 'name' =&amp;gt; 'file2.txt'],
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And even add files from string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$zip-&amp;gt;addFromString('sample.txt', 'Just a sample text');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many more helpful methods available and same applies for &lt;strong&gt;PharAdapter&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mailer Library: Flexible Email Integration with Multiple Adapters
&lt;/h2&gt;

&lt;p&gt;The Mailer library in Quantum PHP Framework 2.9 introduces a robust and flexible email delivery system that supports multiple adapters, allowing you to integrate various email services with ease. Whether you need to send emails via a local SMTP server or use cloud-based email services, this library provides all the necessary tools to get started.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SMTP&lt;/strong&gt;: For sending emails via a local or remote SMTP server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mailgun&lt;/strong&gt;: For sending emails through Mailgun’s email API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mandrill&lt;/strong&gt;: For sending emails through Mandrill’s email delivery service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SendGrid&lt;/strong&gt;: For using SendGrid’s email API to send emails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SendinBlue&lt;/strong&gt;: For leveraging SendinBlue’s SMTP and email API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Setting Up the Mailer:&lt;/p&gt;

&lt;p&gt;To begin, add the corresponding values to the &lt;code&gt;.env&lt;/code&gt; file. If you're using SMTP, you'll need to provide the following settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MAIL_HOST=
MAIL_SMTP_SECURE=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, in the &lt;code&gt;shared/config/mailer.php&lt;/code&gt; file, make sure to specify the current adapter in use. Here's an example of how to set it up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return [
    /**
     * ---------------------------------------------------------
     * Mailer Settings
     * ---------------------------------------------------------
     *
     * Set the current configuration to use.
     */
    'current' =&amp;gt; 'smtp',  // Set the adapter in use

    'smtp' =&amp;gt; [
        'host' =&amp;gt; env('MAIL_HOST'),
        'secure' =&amp;gt; env('MAIL_SMTP_SECURE'),
        'port' =&amp;gt; env('MAIL_PORT'),
        'username' =&amp;gt; env('MAIL_USERNAME'),
        'password' =&amp;gt; env('MAIL_PASSWORD'),
    ],

    'sendinblue' =&amp;gt; [
        'api_key' =&amp;gt; env("SENDINBLUE_APIKEY", null),
    ],

    'sendgrid' =&amp;gt; [
        'api_key' =&amp;gt; env("SENDGRID_APIKEY", null),
    ],

    'mailgun' =&amp;gt; [
        'api_key' =&amp;gt; env("MAILGUN_APIKEY", null),
        'domain' =&amp;gt; env("MAILGUN_DOMAIN", null),
    ],

    'mandrill' =&amp;gt; [
        'api_key' =&amp;gt; env("MANDRILL_APIKEY", null),
    ],
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once all the configurations are set, you can use the &lt;code&gt;mailer()&lt;/code&gt; helper method to compose and send emails. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mailer()
    -&amp;gt;setFrom('johndoe@mail.com', 'John Doe')  
    -&amp;gt;setAddress('ban@mail.com', 'Ban Doe')   
    -&amp;gt;setSubject('Hello')                     
    -&amp;gt;setBody('Hello everyone')                
    -&amp;gt;send();     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  FileSystem Library: Google Drive Adapter
&lt;/h2&gt;

&lt;p&gt;The FileSystem library in Quantum PHP now includes a new &lt;strong&gt;GoogleDrive&lt;/strong&gt;  adapter, alongside the previously available &lt;strong&gt;Local&lt;/strong&gt; and &lt;strong&gt;Dropbox&lt;/strong&gt; adapters. This new adapter allows you to manage files directly on your Google Drive, offering additional flexibility for file storage.&lt;/p&gt;

&lt;p&gt;To use the Local Filesystem adapter, simply create a new instance of &lt;code&gt;FileSystem()&lt;/code&gt;. By default, this will utilize the &lt;code&gt;LocalFileSystemAdapter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For Dropbox or Google Drive, additional setup is required.&lt;/p&gt;

&lt;p&gt;To work with Google Drive, you need to pass the &lt;code&gt;GoogleDriveFileSystemAdapter&lt;/code&gt; as an argument to the &lt;code&gt;FileSystem()&lt;/code&gt; constructor. The &lt;code&gt;GoogleDriveFileSystemAdapter&lt;/code&gt; requires an instance of &lt;code&gt;GoogleDriveApp&lt;/code&gt; which handles essential tasks such as making HTTP requests, managing tokens, and more (Similarly, when using the &lt;code&gt;DropboxFileSystemAdapter&lt;/code&gt;, you'll need to provide a &lt;code&gt;DropboxApp&lt;/code&gt; instance).&lt;/p&gt;

&lt;p&gt;Since token management is the developer’s responsibility, you must also implement the &lt;code&gt;TokenServiceInterface&lt;/code&gt; to handle token-related operations seamlessly.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;TokenServiceInterface&lt;/code&gt; requires the implementation of the following three methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace Quantum\Libraries\Storage\Adapters\Dropbox;

interface TokenServiceInterface
{
    public function getAccessToken(): string;

    public function getRefreshToken(): string;

    public function saveTokens(string $accessToken, ?string $refreshToken = null): bool;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To manage access and refresh tokens, you need to create a &lt;code&gt;TokenService&lt;/code&gt; class that implements the &lt;code&gt;TokenServiceInterface&lt;/code&gt;. This class should be implemented to align with the specific requirements of the application, ensuring proper storage and retrieval of tokens.&lt;/p&gt;

&lt;p&gt;Once your &lt;code&gt;TokenService&lt;/code&gt; class is ready, it should be passed to the &lt;code&gt;GoogleDriveApp&lt;/code&gt;, which will use it to manage token operations effectively. The &lt;code&gt;oogleDriveApp&lt;/code&gt; relies on this service for obtaining access tokens, refreshing them, and saving updated tokens when needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Quantum\Libraries\Storage\Adapters\GoogleDriveFileSystemAdapter;
use Quantum\Libraries\Storage\Adapters\GoogleDriveApp;
use Quantum\Libraries\Storage\FileSystem;
use Quantum\Libraries\Curl\HttpClient;
use Shared\Services\TokenService;

$appKey = config()-&amp;gt;get('filesystem.gdrive.appKey');
$appSecret = config()-&amp;gt;get('filesystem.gdrive.appSecret');

$httpClient = new HttpClient();

$tokenService = new TokenService();

$googleDriveApp = new GoogleDriveApp($appKey, $appSecret, $tokenService, $httpClient);

$googleDriveAdapter = GoogleDriveFileSystemAdapter::getInstance($googleDriveApp);

$fileSystem = new FileSystem($googleDriveAdapter);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the &lt;strong&gt;GoogleDrive&lt;/strong&gt; adapter is set up, you can call any available method to interact with files as usual:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Upload a file
$fileSystem-&amp;gt;put('folder/file.txt', 'This is the file content');

// Retrieve a file's content
$content = $fileSystem-&amp;gt;get('folder/file.txt');

// Delete a file
$fileSystem-&amp;gt;delete('folder/file.txt');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While we’ve explored many aspects of the library in this article, some features, like Module Generation and Pagination, deserve their own deep dives. These are significant features with substantial functionality that require more detailed explanation. Stay tuned, as we’ll provide separate articles on these topics in the near future to help you fully leverage their potential.&lt;/p&gt;

&lt;p&gt;To get started or explore more, visit the &lt;a href="https://github.com/softberg/quantum-php-core" rel="noopener noreferrer"&gt;Quantum PHP Core GitHub repository&lt;/a&gt; for the source code or check out the official &lt;a href="https://quantumphp.io/en" rel="noopener noreferrer"&gt;Quantum PHP website&lt;/a&gt; for detailed documentation and resources.&lt;/p&gt;

&lt;p&gt;We encourage you to explore Quantum PHP Core’s capabilities and see how it can simplify your development journey. Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Database Migrations</title>
      <dc:creator>Arman</dc:creator>
      <pubDate>Mon, 04 Jul 2022 14:09:38 +0000</pubDate>
      <link>https://forem.com/armanist84/database-migrations-2aj9</link>
      <guid>https://forem.com/armanist84/database-migrations-2aj9</guid>
      <description>&lt;p&gt;Suppose we are starting a new big project and of course first thing to do is to compose our database architecture. And we want to use some sort of relational database system such as MySQL.&lt;/p&gt;

&lt;p&gt;Surely we can go ahead and create our database and the tables through some GUI or via terminal. And then we can start codding our project business logic. If on the project works more people than us, then we should share our DB schema with other developers. We can do it by exporting the SQL dump and sending to them to import.&lt;/p&gt;

&lt;p&gt;Sounds good so far…&lt;/p&gt;

&lt;p&gt;Now suppose some developers are adding new columns on some of the tables on their branch. Other developers are removing some columns (as per requirement of their task). Some other developers may change some columns (like changing type or names or indexes, etc.).&lt;/p&gt;

&lt;p&gt;This quickly becomes a mess, as nobody can tell exactly which operations should be taken in the first place, which are in the second and so on…&lt;/p&gt;

&lt;h2&gt;
  
  
  Migrations comes to help
&lt;/h2&gt;

&lt;p&gt;Database migrations or simply migrations, are controlled sets of changes developed to modify the structure of the objects within a relational database. Migrations help transition database schemas from their current state to a new desired state. Whether that involves adding tables and columns, removing elements, splitting fields, or changing types and constraints.&lt;/p&gt;

&lt;p&gt;The goals of database migrations are to make database changes repeatable, shareable, and testable.&lt;br&gt;
In general, migration systems create files that can be shared, applied to multiple database systems, and stored in version control.&lt;/p&gt;
&lt;h2&gt;
  
  
  Migrations in Quantum framework
&lt;/h2&gt;

&lt;p&gt;From version 2.7.0 Quantum comes with new migration feature which allows developers to fill that gap they have previously. It currently supports MySQL 8 and above as well as Mariadb 10.5.x and above. In near future it’s expected to have SQLIte and PostgreSQL included. The way of creating and applying migrations, are done via console commands and it mostly similar to other PHP frameworks if you tried one already.&lt;/p&gt;

&lt;p&gt;The first thing to do is to generate a migration file. which can be done via migration:generate command. The command accepts arguments, "type" and "table name". There are 4 types available for generating migrations, &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;alter&lt;/code&gt;, &lt;code&gt;rename&lt;/code&gt; and &lt;code&gt;drop&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create table migration
&lt;/h2&gt;

&lt;p&gt;To generate &lt;code&gt;create&lt;/code&gt; type of migration we specify the type as first parameter and the table name as second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; php qt migration:generate create users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new migration file will be created in the project migrations directory and will named something like create_table_users_1655307916.php with some prefilled content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php 
use Quantum\Migration\QtMigration; 
use Quantum\Factory\TableFactory; 
use Quantum\Libraries\Database\Schema\Type; 
use Quantum\Libraries\Database\Schema\Key; 
class Create_table_users_1655307916 extends QtMigration 
{ 
    public function up(?TableFactory $tableFactory) 
    {
        $table = $tableFactory-&amp;gt;create('users'); 
    } 
    public function down(?TableFactory $tableFactory) 
    { 
        $tableFactory-&amp;gt;drop('users'); 
    } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Alter table migration
&lt;/h2&gt;

&lt;p&gt;To generate &lt;code&gt;alter&lt;/code&gt; type of migration we specify the type as first parameter and the table name as second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; php qt migration:generate alter users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new migration file will be created in the project migrations directory and will named something like alter_table_users_1655308339.php with some prefilled content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php 
use Quantum\Migration\QtMigration; 
use Quantum\Factory\TableFactory; 
use Quantum\Libraries\Database\Schema\Type; 
use Quantum\Libraries\Database\Schema\Key; 
class Alter_table_users_1655308339 extends QtMigration 
{ 
    public function up(?TableFactory $tableFactory) 
    { 
        $table = $tableFactory-&amp;gt;get('users'); 
    } 
    public function down(?TableFactory $tableFactory) 
    { 
        $table = $tableFactory-&amp;gt;get('users'); 
    } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rename table migration
&lt;/h2&gt;

&lt;p&gt;To generate &lt;code&gt;rename&lt;/code&gt; type of migration we specify the type as first parameter and the table name as second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; php qt migration:generate rename users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new migration file will be created in the project migrations directory and will named something like rename_table_users_1655308655.php with some prefilled content.&lt;/p&gt;

&lt;p&gt;Notice, that the new table name need to be added there as a 2nd argument of rename() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php 
use Quantum\Migration\QtMigration;  
use Quantum\Factory\TableFactory; 
class Rename_table_users_1655308655 extends QtMigration 
{ 
    public function up(?TableFactory $tableFactory) 
    { 
        $tableFactory-&amp;gt;rename('users', $newName); 
    } 
    public function down(?TableFactory $tableFactory) 
    { 
        $tableFactory-&amp;gt;rename($newName, 'users'); 
    } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Drop table migration
&lt;/h2&gt;

&lt;p&gt;To generate &lt;code&gt;drop&lt;/code&gt; type of migration we specify the type as first parameter and the table name as second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; php qt migration:generate drop users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new migration file will be created in the project migrations directory and will named something like drop_table_users_1655308866.php with some prefilled content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php 
use Quantum\Migration\QtMigration; 
use Quantum\Factory\TableFactory; 
class Drop_table_users_1655308866 extends QtMigration 
{ 
    public function up(?TableFactory $tableFactory) 
    { 
        $tableFactory-&amp;gt;drop('users'); 
    } 
    public function down(?TableFactory $tableFactory) 
    { 
        // 
    } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, every time it creates a class which extends the QTMigration base class and it always has 2 public methods: up() and down().&lt;/p&gt;

&lt;p&gt;The up method is used to create new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating migration file content
&lt;/h2&gt;

&lt;p&gt;Ok, now let’s open our migration file and add some new columns to the table with addColumn() method..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function up(?TableFactory $tableFactory) 
{ 
    $table = $tableFactory-&amp;gt;create('users'); 
    $table-&amp;gt;addColumn('id', Type::INT, 11)-&amp;gt;autoIncrement(); 
    $table-&amp;gt;addColumn('username', Type::VARCHAR, 100)-&amp;gt;unique(); 
    $table-&amp;gt;addColumn('is_active', Type::BOOL); 
    $table-&amp;gt;addColumn('options', Type::ENUM, ['op1', 'op2', 'op3'])-&amp;gt;default('op2'); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have added 3 columns to the table:&lt;/p&gt;

&lt;p&gt;id as integer with autoincrement&lt;br&gt;
username as varchar with unique index&lt;br&gt;
options as enum with default value&lt;br&gt;
We can also alter the existing column paramters with modifyColumn() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function up(?TableFactory $tableFactory) 
{ 
    $table = $tableFactory-&amp;gt;get('users'); 
    $table-&amp;gt;modifyColumn('username', Type::VARCHAR, 50); 
    $table-&amp;gt;modifyColumn('is_active', Type::TINYINT, 1); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have modified 2 columns:&lt;/p&gt;

&lt;p&gt;username column length was changed from 100 to 50&lt;br&gt;
is_active column type was changed from boolean to tinyint&lt;br&gt;
We can also rename columns with renameColumn() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function up(?TableFactory $tableFactory) 
{ 
    $table = $tableFactory-&amp;gt;get('users'); 
    $table-&amp;gt;renameColumn('username', 'email'); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To drop columns we use dropColumn() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function up(?TableFactory $tableFactory) 
{ 
    $table = $tableFactory-&amp;gt;get('users'); 
    $table-&amp;gt;dropColumn('options'); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Playing with indexes
&lt;/h2&gt;

&lt;p&gt;When adding new column we can also specify the index with it as well, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;addColumn('profile_id', Type::INT, 11)-&amp;gt;index();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also we can specify the index name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;addColumn('profile_id', Type::INT, 11)
      -&amp;gt;index('idx_profile_id');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 5 types of indexes are available: primary, index, unique, fulltext and spatial.&lt;/p&gt;

&lt;p&gt;In order to add index key to an existing column we can use addIndex() method like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;addIndex('profile_id', Key::INDEX);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or with the index name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;addIndex('profile_id', Key::INDEX, 'idx_profile_id');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, if you are not specifying the index name, it will take the column name is its key name.&lt;/p&gt;

&lt;p&gt;To drop the index from the column we can use dropIndex() method by passing the index name as paramter like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;dropIndex('idx_profile_id');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Migrating migrations
&lt;/h2&gt;

&lt;p&gt;Ok, now when we have our table schemas defined in the migration files, it’s time to migrate them.&lt;/p&gt;

&lt;p&gt;So to apply the migrations we simply run this command (the argument up is optional and can be skipped):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; php qt migration:migrate up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you running this command first time, it will create new table in your database with name migrations, where it will store all applied migrations. Then it will iterate through the migrations which were created and run them all in chronological order.&lt;/p&gt;

&lt;p&gt;Notice that the migration:migrate command runs only up migrations and only ones that weren't yet migrated.&lt;/p&gt;

&lt;p&gt;If you want to roll back the migrated migrations you will need to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; php qt migration:migrate down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be aware that this will iterate through all the migrations in reverse chronological order and run down migrations. If you want to roll back not all the migrations but down to specific migration, you can specify the step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; php qt migration:migrate down --step=2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is all for today, hope you find it useful, write comments if you have questions… Good luck.&lt;/p&gt;

</description>
      <category>quantumphp</category>
      <category>php</category>
      <category>framework</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
