updated and simplified examples
This commit is contained in:
parent
0d1677a9b4
commit
110c11dbb2
5 changed files with 70 additions and 179 deletions
|
@ -2,30 +2,33 @@
|
|||
|
||||
use Destructr\Factory;
|
||||
|
||||
class ExampleFactory extends Factory {
|
||||
class ExampleFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Example factory with a different schema, to index on random_data, but not
|
||||
* by dso_type.
|
||||
*
|
||||
* Also uses a different column name for dso.id
|
||||
* Example factory with a different schema, to index on random_data for faster searching
|
||||
*/
|
||||
protected $schema = [
|
||||
'dso.id' => [
|
||||
'name'=>'dso_id_other_name',
|
||||
'type'=>'VARCHAR(16)',
|
||||
'name' => 'dso_id', //column name to be used
|
||||
'type' => 'VARCHAR(16)', //column type
|
||||
'index' => 'BTREE', //whether/how to index
|
||||
'unique' => true, //whether column should be unique
|
||||
'primary' => true, //whether column should be the primary key
|
||||
],
|
||||
'dso.type' => [
|
||||
'name' => 'dso_type',
|
||||
'type' => 'VARCHAR(30)',
|
||||
'index' => 'BTREE',
|
||||
'unique' => true,
|
||||
'primary' => true
|
||||
],
|
||||
'dso.deleted' => [
|
||||
'name'=>'dso_deleted',
|
||||
'type'=>'INT',
|
||||
'index'=>'BTREE'
|
||||
'name' => 'dso_deleted',
|
||||
'type' => 'BIGINT',
|
||||
'index' => 'BTREE',
|
||||
],
|
||||
'random_data' => [
|
||||
'name'=>'random_data',
|
||||
'type'=>'VARCHAR(100)',
|
||||
'index'=>'BTREE'
|
||||
]
|
||||
'name' => 'random_data',
|
||||
'type' => 'VARCHAR(64)',
|
||||
'index' => 'BTREE',
|
||||
],
|
||||
];
|
||||
}
|
50
examples/example_use.php
Normal file
50
examples/example_use.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
/**
|
||||
* This file demonstrates some basic uses of Destructr, from the creation
|
||||
* of a connection and factory, through to creating, inserting, updating,
|
||||
* deleting, and querying data.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
$driver = \Destructr\DriverFactory::factory(
|
||||
'sqlite:' . __DIR__ . '/example.sqlite'
|
||||
);
|
||||
|
||||
/*
|
||||
Creates a factory using the table 'example_table', and creates
|
||||
the necessary table. Note that prepareEnvironment() can safely be called
|
||||
multiple times. updateEnvironment shouldn't be used this way in production,
|
||||
as if it is called more than once per second during a schema change, errors
|
||||
may be introduced.
|
||||
*/
|
||||
include __DIR__ . '/example_factory.php';
|
||||
$factory = new ExampleFactory($driver, 'example_table');
|
||||
$factory->prepareEnvironment();
|
||||
$factory->updateEnvironment();
|
||||
|
||||
/*
|
||||
Inserting a record
|
||||
*/
|
||||
$obj = $factory->create(
|
||||
[
|
||||
'dso.type'=>'foobar',
|
||||
'random_data' => md5(rand())
|
||||
]
|
||||
);
|
||||
$obj->insert();
|
||||
|
||||
/*
|
||||
Search by random data field, which is indexed due to the
|
||||
ExampleFactory class' $schema property.
|
||||
*/
|
||||
$search = $factory->search();
|
||||
$search->where('${random_data} LIKE :q');
|
||||
$result = $search->execute(['q'=>'ab%']);
|
||||
foreach($result as $r) {
|
||||
var_dump($r->get());
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
<?php
|
||||
include __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
/*
|
||||
Constructing MariaDB drivers should be done using factoryFromPDO,
|
||||
so that they use the MariaDB driver instead of the MySQL driver.
|
||||
*/
|
||||
$driver = \Destructr\DriverFactory::factoryFromPDO(
|
||||
new \PDO(
|
||||
'mysql:server=localhost;port=3307;dbname=destructr',
|
||||
'root'
|
||||
),
|
||||
'mariadb'
|
||||
);
|
||||
|
||||
/*
|
||||
Creates a factory using the table 'example_table', and creates
|
||||
the necessary table. Note that prepareEnvironment() can safely be called
|
||||
multiple times.
|
||||
*/
|
||||
include __DIR__ . '/example_factory.php';
|
||||
$factory = new ExampleFactory($driver, 'example_table');
|
||||
$factory->prepareEnvironment();
|
||||
$factory->updateEnvironment();
|
||||
|
||||
/*
|
||||
The following can be uncommented to insert dummy records
|
||||
into the given table.
|
||||
*/
|
||||
// for($i = 0; $i < 10; $i++) {
|
||||
// $obj = $factory->create(
|
||||
// [
|
||||
// 'dso.type'=>'foobar',
|
||||
// 'random_data' => md5(rand())
|
||||
// ]
|
||||
// );
|
||||
// $obj->insert();
|
||||
// }
|
||||
|
||||
/*
|
||||
Search by random data field
|
||||
*/
|
||||
$search = $factory->search();
|
||||
$search->where('${random_data} = :q');
|
||||
$result = $search->execute(['q'=>'rw7nivub9bhhh3t4']);
|
||||
|
||||
/*
|
||||
Search by dso.id, which is much faster because it's indexed
|
||||
*/
|
||||
// $search = $factory->search();
|
||||
// $search->where('${dso.id} = :q');
|
||||
// $result = $search->execute(['q'=>'rw7nivub9bhhh3t4']);
|
|
@ -1,52 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Destructr\Factory;
|
||||
|
||||
include __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
/*
|
||||
Constructing MariaDB drivers should be done using factoryFromPDO,
|
||||
so that they use the MariaDB driver instead of the MySQL driver.
|
||||
*/
|
||||
$driver = \Destructr\DriverFactory::factory(
|
||||
'mysql:server=localhost;port=3306;dbname=destructr',
|
||||
'root'
|
||||
);
|
||||
|
||||
/*
|
||||
Creates a factory using the table 'example_table', and creates
|
||||
the necessary table. Note that prepareEnvironment() can safely be called
|
||||
multiple times.
|
||||
*/
|
||||
include __DIR__ . '/example_factory.php';
|
||||
$factory = new Factory($driver, 'example_table');
|
||||
$factory->prepareEnvironment();
|
||||
$factory->updateEnvironment();
|
||||
|
||||
/*
|
||||
The following can be uncommented to insert dummy records
|
||||
into the given table.
|
||||
*/
|
||||
// for($i = 0; $i < 100; $i++) {
|
||||
// $obj = $factory->create(
|
||||
// [
|
||||
// 'dso.type'=>'foobar',
|
||||
// 'random_data' => md5(rand())
|
||||
// ]
|
||||
// );
|
||||
// $obj->insert();
|
||||
// }
|
||||
|
||||
/*
|
||||
Search by random data field
|
||||
*/
|
||||
// $search = $factory->search();
|
||||
// $search->where('${random_data} = :q');
|
||||
// $result = $search->execute(['q'=>'rw7nivub9bhhh3t4']);
|
||||
|
||||
/*
|
||||
Search by dso.id, which is much faster because it's indexed
|
||||
*/
|
||||
// $search = $factory->search();
|
||||
// $search->where('${dso.id} = :q');
|
||||
// $result = $search->execute(['q'=>'rw7nivub9bhhh3t4']);
|
|
@ -1,58 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Destructr\Factory;
|
||||
|
||||
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.
|
||||
*/
|
||||
$driver = \Destructr\DriverFactory::factory(
|
||||
'sqlite:' . __DIR__ . '/example.sqlite'
|
||||
);
|
||||
|
||||
/*
|
||||
Creates a factory using the table 'example_table', and creates
|
||||
the necessary table. Note that prepareEnvironment() can safely be called
|
||||
multiple times.
|
||||
*/
|
||||
include __DIR__ . '/example_factory.php';
|
||||
$factory = new Factory($driver, 'example_table');
|
||||
$factory->prepareEnvironment();
|
||||
$factory->updateEnvironment();
|
||||
|
||||
/*
|
||||
The following can be uncommented to insert dummy records
|
||||
into the given table.
|
||||
*/
|
||||
// ini_set('max_execution_time','0');
|
||||
// for($i = 0; $i < 10; $i++) {
|
||||
// $obj = $factory->create(
|
||||
// [
|
||||
// 'dso.type'=>'foobar',
|
||||
// 'random_data' => md5(rand())
|
||||
// ]
|
||||
// );
|
||||
// $obj->insert();
|
||||
// }
|
||||
|
||||
/*
|
||||
Search by random data field
|
||||
*/
|
||||
$search = $factory->search();
|
||||
$search->where('${random_data} LIKE :q');
|
||||
$result = $search->execute(['q'=>'%ab%']);
|
||||
foreach($result as $r) {
|
||||
var_dump($r->get());
|
||||
$r['random_data_2'] = md5(rand());
|
||||
$r->update();
|
||||
}
|
||||
|
||||
/*
|
||||
Search by dso.id, which is much faster because it's indexed
|
||||
*/
|
||||
// $search = $factory->search();
|
||||
// $search->where('${dso.id} = :q');
|
||||
// $result = $search->execute(['q'=>'rw7nivub9bhhh3t4']);
|
Loading…
Reference in a new issue