1. Go to this page and download the library: Download popphp/pop-db 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/ */
use Pop\Db\Db;
use Pop\Db\Record;
$db = Db::mysqlConnect([
'database' => 'DATABASE',
'username' => 'DB_USER',
'password' => 'DB_PASS'
]);
class Users extends Record {}
Record::setDb($db);
use Pop\Db\Db;
use Pop\Db\Record;
$db = Db::mysqlConnect([
'database' => 'DATABASE',
'username' => 'DB_USER',
'password' => 'DB_PASS'
]);
$dbUsers = Db::mysqlConnect([
'database' => 'DATABASE_FOR_USERS',
'username' => 'DB_USER',
'password' => 'DB_PASS'
]);
class Users extends Record {};
Users::setDb($dbUsers); // Only the users table class uses the $dbUsers connection
Record::setDb($db); // All other table classes will use the $db connection
// Fetch a single user record by ID
$user = Users::findById(1);
// Search for a single user record
$user = Users::findOne(['username' => 'testuser']);
// Search for a single user record, or create one if it doesn't exist
$user = Users::findOneOrCreate(['username' => 'testuser']);
// Search for the latest single user record
$user = Users::findLatest();
// Search for the latest single user record by 'last_login'
$user = Users::findLatest('last_login');
$user->username = 'newusername';
$user->save();
$user->delete();
$user->increment('attempts'); // Increment column by one and save
$user->decrement('capacity', 5); // Decrement column by 5 and save
// Make a new copy of the user record in the database
// The $replace parameter can be an array of new, overriding column values
$newUser = $user->copy($replace);
$users = Users::findBy(['id' => 1]); // WHERE id = 1
$users = Users::findBy(['id!=' => 1]); // WHERE id != 1
$users = Users::findBy(['id>' => 1]); // WHERE id > 1
$users = Users::findBy(['id>=' => 1]); // WHERE id >= 1
$users = Users::findBy(['id<' => 1]); // WHERE id < 1
$users = Users::findBy(['id<=' => 1]); // WHERE id <= 1
$users = Users::findBy(['%username%' => 'test']); // WHERE username LIKE '%test%'
$users = Users::findBy(['username%' => 'test']); // WHERE username LIKE 'test%'
$users = Users::findBy(['%username' => 'test']); // WHERE username LIKE '%test'
$users = Users::findBy(['-%username' => 'test']); // WHERE username NOT LIKE '%test'
$users = Users::findBy(['username%-' => 'test']); // WHERE username NOT LIKE 'test%'
$users = Users::findBy(['-%username%-' => 'test']); // WHERE username NOT LIKE '%test%'
$users = Users::findBy(['username' => null]); // WHERE username IS NULL
$users = Users::findBy(['username-' => null]); // WHERE username IS NOT NULL
$users = Users::findBy(['id' => [2, 3]]); // WHERE id IN (2, 3)
$users = Users::findBy(['id-' => [2, 3]]); // WHERE id NOT IN (2, 3)
$users = Users::findBy(['id' => '(1, 5)']); // WHERE id BETWEEN (1, 5)
$users = Users::findBy(['id-' => '(1, 5)']); // WHERE id NOT BETWEEN (1, 5)
class Users extends Pop\Db\Record
{
/**
* Mock Schema
* - id
* - role_id (FK to roles.id)
* - username
* - password
* - email
*/
// Define the 1:1 relationship of the user's role
public function role()
{
return $this->hasOneOf('Roles', 'role_id');
}
// Define the 1:1 relationship of the info record this user owns
public function info()
{
return $this->hasOne('Info', 'user_id')
}
// Define the 1:many relationship to all the orders this user owns
public function orders()
{
return $this->hasMany('Orders', 'user_id');
}
}
class Roles extends Pop\Db\Record
{
/**
* Mock Schema
* - id (FK to users.role_id)
* - role
*/
}
class Info extends Pop\Db\Record
{
/**
* Mock Schema
* - user_id (FK to users.id)
* - address
* - phone
*/
// Define the parent relationship up to the user that owns this info record
public function user()
{
return $this->belongsTo('Users', 'user_id');
}
}
class Orders extends Pop\Db\Record
{
/**
* Mock Schema
* - id
* - user_id (FK to users.id)
* - order_date
* - order_total
* - products
*/
// Define the parent relationship up to the user that owns this order record
public function user()
{
return $this->belongsTo('Users', 'user_id');
}
}
// The two 1:1 relationships
$user = Users::findById(1);
print_r($user->role()->toArray());
print_r($user->info()->toArray());
use Pop\Db\Sql\Migrator;
Migrator::create('MyNewMigration', __DIR__ . 'migrations');
use Pop\Db\Sql\Migration\AbstractMigration;
class MyNewMigration extends AbstractMigration
{
public function up()
{
}
public function down()
{
}
}
use Pop\Db\Sql\Migration\AbstractMigration;
class MyNewMigration extends AbstractMigration
{
public function up()
{
$schema = $this->db->createSchema();
$schema->create('users')
->int('id', 16)->increment()
->varchar('username', 255)
->varchar('password', 255)
->primary('id');
$schema->execute();
}
public function down()
{
$schema = $this->db->createSchema();
$schema->drop('users');
$schema->execute();
}
}
use Pop\Db\Db;
use Pop\Db\Sql\Migrator;
$db = Pop\Db\Db::connect('mysql', [
'database' => 'my_database',
'username' => 'my_db_user',
'password' => 'my_db_password',
'host' => 'mydb.server.com'
]);
$migrator = new Migrator($db, 'migrations');
$migrator->run();
use Pop\Db\Db;
use Pop\Db\Sql\Migrator;
$db = Pop\Db\Db::connect('mysql', [
'database' => 'my_database',
'username' => 'my_db_user',
'password' => 'my_db_password',
'host' => 'mydb.server.com'
]);
$migrator = new Migrator($db, 'migrations');
$migrator->rollback();
use Pop\Db\Sql\Seeder;
Seeder::create('MyFirstSeeder', __DIR__ . '/seeds');
use Pop\Db\Adapter\AbstractAdapter;
use Pop\Db\Sql\Seeder\AbstractSeeder;
class MyFirstSeeder extends AbstractSeeder
{
public function run(AbstractAdapter $db): void
{
}
}