destructr/examples/sqlite.php

59 lines
1.4 KiB
PHP
Raw Normal View History

2020-08-26 17:53:27 +00:00
<?php
use Destructr\Factory;
2020-08-26 17:53:27 +00:00
include __DIR__ . '/../vendor/autoload.php';
/*
SQLite drivers can be created by the default factory.
A charset of UTF8 should be specified, to avoid character encoding
issues.
*/
2020-08-26 17:53:27 +00:00
$driver = \Destructr\DriverFactory::factory(
'sqlite:' . __DIR__ . '/example.sqlite'
2020-08-26 17:53:27 +00:00
);
/*
Creates a factory using the table 'example_table', and creates
the necessary table. Note that prepareEnvironment() can safely be called
2020-08-26 17:53:27 +00:00
multiple times.
*/
include __DIR__ . '/example_factory.php';
$factory = new Factory($driver, 'example_table');
$factory->prepareEnvironment();
$factory->updateEnvironment();
2020-08-26 17:53:27 +00:00
/*
The following can be uncommented to insert dummy records
2020-08-26 17:53:27 +00:00
into the given table.
*/
2020-08-26 17:53:27 +00:00
// ini_set('max_execution_time','0');
// for($i = 0; $i < 10; $i++) {
2020-08-26 17:53:27 +00:00
// $obj = $factory->create(
// [
// 'dso.type'=>'foobar',
// 'random_data' => md5(rand())
// ]
// );
// $obj->insert();
// }
/*
Search by random data field
*/
2020-08-26 17:53:27 +00:00
$search = $factory->search();
2020-08-27 17:42:11 +00:00
$search->where('${random_data} LIKE :q');
$result = $search->execute(['q'=>'%ab%']);
foreach($result as $r) {
var_dump($r->get());
2020-08-27 17:42:11 +00:00
$r['random_data_2'] = md5(rand());
$r->update();
}
2020-08-26 17:53:27 +00:00
/*
Search by dso.id, which is much faster because it's indexed
*/
2020-08-26 17:53:27 +00:00
// $search = $factory->search();
// $search->where('${dso.id} = :q');
// $result = $search->execute(['q'=>'rw7nivub9bhhh3t4']);