PHP code example of wscore / scoredb

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

    

wscore / scoredb example snippets


DB::config( [
    'dsn'  => 'mysql:host=localhost;dbname=name;charset=utf',
    'user' => 'username',
    'pass' => 'password',
    'option' => [],
    'attribute' => []
] );

DB::config( [
    'dsn'  => '...',
] );
DB::config( [
    'dsn'  => '...',
    'for'  => 'write',
] );

$pdo = DB::connect();
$pdo2 = DB::connectWrite();

DB::config( 'log', [
    'dsn' => 'mysql',...
] );
// then get PDO as:
$pdo = DB::connect( 'log' );

$result = DB::query( 'myTable' )
    ->connect( 'conn' )
    ->where( DB::given( 'status' )->is( 5 ) )
    ->select();

class YourDao extends Dao
{
    protected $fetch_class = 'WScore\ScoreDB\Entity\ActiveRecord';
}

/**
 * @method User status( $status=1 )
 */
class User extends Dao
{
    protected $table = 'dao_user';
    protected $keyName = 'user_id';
    protected $timeStamps = [
        'created_at' => [
            'created_at',
            'open_date' => 'Y-m-d'
        ],
        'updated_at' => [
            'updated_at'
        ],
    ];

    /**
     * @param int $status
     */
    public function scopeActive() {
        $this->where( $this->status->is( 1 ) );
    }
}

class Other extends OtherQuery
{
    user DaoTrait;
}

// list all users with status=1.
$found = $user->where(
    $user->status->is(1)
)->select();

$users = User::forge();
foreach( $users as $user ) {
    echo $user->name;
}

// update active people to status=2.
$user->active()->update('status'=>2);

$user->status = 2;
$user->active()->update();

$user->insert( [ 'name' => 'bob', 'status'=>0 ] );

$user->name = 'bob';
$user->status = 0;
$user->insert();

$user = User::forge();
// use $dao as iterator.
foreach( $user->active() as $applied ) {
    echo $applied->name;
}

class User extends
    public function on{EventName}Hook( $data ) {
    }
}

class User extends
    public function on{EventName}Filter( $data ) {
        return $data;
    }
}

// $hookObject has the events and scopes.
$user->setHook( $hookObject );

class YourDao extends Dao
{
    public function getTargetsRelation() {
        return Relation::HasOne( $this, 'TargetDaoName' );
    }
}
$dao     = new YourDao();
$entity  = $dao->find($id);

// get target record using HasOne relation.
$targets = $dao->getTargetsRelation()->entity( $entity )->get();

// or use EntityObject's magic method.
$targets = $entity->targets->get();

$entity->targets->link( $targetEntity );

DB::db()->transaction( function() {
    // do database access.
} );