PHP code example of mgcosta / spanner-orm-builder

1. Go to this page and download the library: Download mgcosta/spanner-orm-builder 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/ */

    

mgcosta / spanner-orm-builder example snippets



use MgCosta\Spanner\Manager;
use Google\Cloud\Spanner\Database;

// $database = your database instance for google cloud spanner;
// instance of Google\Cloud\Spanner\Database;

$manager = new Manager($database);
$manager->boot();


use MgCosta\Spanner\Model\Model;

class User extends Model {}

$users = User::where('age', '>', 30)->get();

$id = 1;
$user = User::find($id);


use MgCosta\Spanner\Model\Model;

class User extends Model {}

// deleting
User::where('id', 1)->delete();

// updating
$status = User::where('id', 5)->update(['name' => 'Richard', 'age' => 30]);


use MgCosta\Spanner\Model\Model;

class User extends Model {

    protected $primaryKey = 'userId';
    
    // available strategies [uuid4, increment] 
    // increment is not recommend by cloud spanner
    protected $keyStrategy = 'uuid4';
    
    // we must define the properties which corresponds to the columns of the table as public
    public $userId;
    public $name;
    public $age;
    public $email;
}

$user = new User();
$user->name = 'Miguel';
$user->age = 28;
$user->email = '[email protected]';
$user->save();


use MgCosta\Spanner\Facade\SpannerDB;

(new SpannerDB())->table('users')->whereIn('id', [1, 2, 3])->get();

// you can also provide a custom spanner Database Instance
// $database = instance of Google\Cloud\Spanner\Database;
(new SpannerDB($database))->table('users')->where('id', 1)->first();