PHP code example of softwarepunt / instarecord

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

    

softwarepunt / instarecord example snippets




class User extends Model
{
    public int $id;
    public string $email;
    public ?string $name;
}

$user = new User();
$user->email = "[email protected]";
$user->save(); 

echo "Created user #{$user->id}!";



use SoftwarePunt\Instarecord\Instarecord;

$config = Instarecord::config();
$config->charset = "utf8mb4";
$config->unix_socket = "/var/run/mysqld/mysqld.sock";
$config->username = "my_user";
$config->password = "my_password";
$config->database = "my_database";
$config->timezone = "UTC";



use SoftwarePunt\Instarecord\Model;

class Car extends Model
{
    public int $id;
    public string $make;
    public string $model;
    public int $year;
}

$car = new Car();
$car->make = "Toyota";
$car->model = "Corolla";
$car->year = 2005;
$car->save(); // INSERT INTO cars [..]

// Post insert, the primary key (id) is automatically populated

$car->year = 2006;
$car->save(); // UPDATE cars SET year = 2006 WHERE id = 123

$car->delete(); // DELETE FROM cars WHERE id = 123

$matchingCars = Car::query()
    ->where('make = ?', 'Toyota')
    ->andWhere('year > ?', 2000)
    ->orderBy('year DESC')
    ->limit(10)
    ->queryAllModels(); // Car[]

$carsPerYear = Instarecord::query()
    ->select('year, COUNT(*) as count')
    ->from('cars')
    ->groupBy('year')
    ->queryKeyValueArray(); // [2005 => 10, 2006 => 5, ..]