1. Go to this page and download the library: Download hiraeth/turso 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/ */
hiraeth / turso example snippets
$database = new Hiraeth\Turso\Database(
new GuzzleHttp\Client(),
'https://<dbname>-<organization>.turso.io',
'Bearer <token>'
);
$result = $database->execute(
"SELECT * FROM users"
);
$result = $database->execute(
"SELECT * FROM users WHERE email LIKE '%@hiraeth.dev'"
);
$result = $database->execute(
"SELECT * FROM @table WHERE email LIKE {domain}",
[
'domain' => '%@hiraeth.dev'
],
[
'table' => 'users'
]
);
if ($result->isError()) {
//
// handle the error
//
}
$result->throw('Failed executing the query');
foreach ($result as $record) {
echo sprintf(
'User with e-mail %s has an ID of %s',
$record->email,
$record->id
);
}
$record = $result->getRecord(0);
$records = $result->getRecords();
$count = count($result);
use Hiraeth\Turso\Types;
class User extends Hiraeth\Turso\Entity
{
const table = 'users';
const identity = [
'id'
];
const types = [
'dateOfBirth' => Types\Date::class
];
protected $id;
public $firstName;
public $lastName;
public $email;
public $dateOfBirth;
public function fullName()
{
return trim(sprintf(
'%s %s',
$this->firstName,
$this->lastName
));
}
}
$records = $result->of(User::class);
foreach ($result->of(User::class) as $user) {
echo sprintf(
'%s has an e-mail of %s' . PHP_EOL,
$user->fullName(),
$user->email
);
}
namespace Hiraeth\Turso\Types;
use DateTime;
/**
* Handles dates
*/
class Date {
/**
* Convert a value from the database to the entity
*/
static public function from(string|null $date): DateTime|null
{
return $date ? new DateTime($date) : NULL;
}
/**
* Convert a value from an entity to the database
*/
static public function to(DateTime|null $date): string|null
{
return $date ? $date->format('Y-m-d') : NULL;
}
}
public function occupation()
{
return $this(Occupation::class)->hasOne(
[
'occupation' => 'id'
],
FALSE
);
}
$this(Occupation::class)
[
'occupation' => 'id'
]
public function occupation(bool $refresh = FALSE): ?Occupation
{
return $this(Occupation::class)->hasOne(
[
'occupation' => 'id'
],
$refresh
);
}
public function users(bool $refresh = FALSE): Result
{
return $this(User::class)->hasMany(
[
'id' => 'occupation'
],
$refresh
);
}
if (count($occupation->users())) {
foreach ($occupation->users() as $user) {
// ...
}
}
public function friends($bool $refresh = FALSE): Result
{
return $this(User::class, 'friends')->hasMany(
[
'id' => 'user', 'friend' => 'id'
],
$refresh
);
}
public function occupation(bool|Occupation $refresh = FALSE): ?Occupation
{
if ($refresh instanceof Occupation) {
$this(Occupation::class)->changeOne($refresh, [
'id' => 'occupation'
]);
}
return $this(Occupation::class)->hasOne(
[
'occupation' => 'id'
],
$refresh
);
}
/**
* Set the "ORDER BY" portion of the statement
*/
public function order(Query ...$sorts): static
{
if (empty($sorts)) {
$clause = $this('');
} else {
$clause = $this('ORDER BY @sorts')->bind(', ', FALSE)->raw('sorts', $sorts);
}
$this->raw('order', $clause);
return $this;
}