PHP code example of oza / database

1. Go to this page and download the library: Download oza/database 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/ */

    

oza / database example snippets

 

 
 return [
     'driver' => 'mysql',
     'name' => 'db_package',
     'host' => 'localhost:3306',
     "username" => "root",
     "password" => "",
     "migrations" => [
         "folder" => __DIR__ . '/../migrations/' // path of migrations folder ( #important )
     ]
 ];

   
   // After registered your composer autoloader (e.g: ance()->register(__DIR__.'/../db.php');
   

    
    
    use OZA\Database\Migrations\Interfaces\MigrationTableInterface;
    use OZA\Database\Migrations\Schema\Schema;
    use OZA\Database\Migrations\Table;
    
    class CreateTestUsersMigration implements MigrationTableInterface
    {
    
        /**
         * Create table
         *
         * @param Schema $schema
         * @return mixed
         */
        public function up(Schema $schema): void
        {
            $schema->create('users', function (Table $table) {
    
                $table->integer('id')->autoIncrement()->index();
    
                $table->timestamps();
            });
        }
    
        /**
         * Called when rollback
         *
         * @param Schema $schema
         * @return mixed
         */
        public function down(Schema $schema): void
        {
            $schema->drop('users');
        }
    }
    

     
     
     use OZA\Database\Migrations\Interfaces\MigrationTableInterface;
     use OZA\Database\Migrations\Schema\Schema;
     use OZA\Database\Migrations\Table;
     
     class CreateTestUsersMigration implements MigrationTableInterface
     {
     
         /**
          * Create table
          *
          * @param Schema $schema
          * @return mixed
          */
         public function up(Schema $schema): void
         {
             $schema->create('users', function (Table $table) {
     
                 $table->integer('id')->autoIncrement()->index();
                 $table->string('name')->index();
                 $table->string('email')->index()->unique();
                 $table->string('password');
                 $table->timestamps();
             });
         }
     
         /**
          * Called when rollback
          *
          * @param Schema $schema
          * @return mixed
          */
         public function down(Schema $schema): void
         {
             $schema->drop('users');
         }
     }
     

    
    $user = \OZA\Database\Facade\Query::table('users')->find(2);





namespace OZA\Database\Tests\Entities;


use OZA\Database\Query\Entity;
use OZA\Database\Query\Relations\ManyToOne;

class UserEntity extends Entity
{

    /**
     * @param string $value
     * @return mixed
     */
    public function getEmailAttribute(string $value)
    {
        return strtoupper($value);
    }

    public function setNameAttribute($value)
    {
        return strtoupper($value);
    }

    /**
     * @return ManyToOne
     */
    public function posts()
    {
        return $this->manyToOne(PostEntity::class, 'user_id', 'id');
    }
}

 
 // Create new User
 \App\Entities\UserEntity::make([
     'name' => 'Aboubacar',
     'email' => '[email protected]'
])->create();
 
 // Find a User
 \App\Entities\UserEntity::query()->find(2);
 
 // Count
  \App\Entities\UserEntity::query()->count();
  
  // With Where clause
  \App\Entities\UserEntity::query()->where('name', 'aboubacar')->get();
  
  // First item
  \App\Entities\UserEntity::query()->where('name', 'LIKE', '%bouba%')->first();
  
  \App\Entities\UserEntity::query()->where(function (\OZA\Database\Query\QueryBuilder $builder) {
     $builder->where('name', 'aboubacar')
     ->orWhere('name', 'oza')
     ->get(); 
  });
 
bash
        php vendor/bin/database migration:create create_users_table --table=users
    
bash
   php vendor/bin/database migrate
  
bash
   php vendor/bin/database migrate:rollback