PHP code example of mwd / marquee

1. Go to this page and download the library: Download mwd/marquee library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mwd / marquee example snippets




* Library Use Statements
 */
use Marquee\Core\Connection\MySQLConnection;
use Marquee\Data\Entity;
use Marquee\Exception\Exception;
use Marquee\Schema\Property;

/**
 * PHP Core Use Statements
 */
use \Generator;

/**
 * Define a sample entity with two string properties:
 * - username
 * - password
 */
class User extends Entity
{
    public function getUsername(): string
    {
        return $this->username;
    }

    public static function Properties(): Generator
    {
        yield Property::string('username')->unique();
        yield Property::string('password');
    }
}

$db = new MySQLConnection(MySQLConnection::CreateDsn(DB_HOST, DB_PORT, DB_PASSWORD, DB_USERNAME));
$db->selectDb(DB_NAME);

if ($db->tryConnect($e)) {
    try {
        /**
         * Build the user table if it doesn't already exist in the schema
         */
        $table = User::BuildTable($db);
        if (!$table->exists()) {
            $table->create();
            echo 'Created user table', '<br>';
        }

        /**
         * Query all users
         */
        $userCount = 0;
        $users     = $db->query(User::class)->limit(10)->get();

        echo '<ul>';
        while ($user = $users->next()) {
            echo '<li>', $user, '</li>';
            $userCount++;
        }
        echo '</ul>';

        /**
         * If we have less than 10 users, insert a new one.
         */
        if ($userCount < 10) {
            $insert = $db->query(User::class)->create([
                'username' => 'Test ' . uniqid(),
                'password' => password_hash('test password', PASSWORD_ARGON2I)
            ]);

            if ($user = $insert->next()) {
                echo 'Created test user';
            }
        } else {
            /**
             * Start over if we have 10 users
             */
            $db->query(User::class)->truncate()->next();
            echo 'Deleted all users';
        }
    } catch (Exception $e) {
        echo 'Error: ', $e->getMessage();
    } finally {
        $db->disconnect();
    }
} else {
    exit($e->getMessage());
}