1. Go to this page and download the library: Download reneschmidt/simpleorm 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/ */
$sample = Sample::getInst()->findOneBy("some_name", "abc"); // returns record of type "Sample"
print($sample->get("some_name")); // prints "abc"
$samples = Sample::getInst()->findBy("some_name", "abc"); // returns array with "Sample" items
$samples = Sample::getInst()->findBy("some_name", "abc", \PDO\FETCH_ASSOC); // returns array with "Sample" array
foreach($samples AS $sample) {
print($sample->get("some_name")); // prints "abc"
}
$samples = Sample::getInst()->findByQuery("SELECT * FROM sample WHERE some_name = ?", ["abc"]);
foreach($samples AS $sample) {
print($sample->get("some_name")); // prints "abc"
}
// apply filter
$samples = Sample::getInst()->setFilter(function($inst) {
$inst['some_name'] = $inst['some_name'] . 'x'; // apply filter to array
return $inst;
})->findByQuery("SELECT * FROM sample WHERE some_name = ?", ["abc"]);
foreach($samples AS $sample) {
print($sample->get("some_name")); // prints "abcx"
}
xample MySQL database on localhost
$dsn = 'mysql:host=localhost;port=3306;dbname=wordpress';
// For MySQL, also define user name and password. Not used for Sqlite.
$user = 'username';
$pass = 'password';
use \SimpleOrm\SimpleOrm;
$simpleDb = SimpleDb::getInst($dsn, $user, $pass);
// You need to provide your own implementation of SimpleDbConfig (here we use WpDbConfig)
$wpDbConfig = WpDbConfig::getInst($simpleDb);
// Setup will create the database and the tables according to your SampleDbConfig implementation
// Obviously you want this to execute only during installation of the app.
$wpDbConfig->setUp();
/**
* WpUserMeta Model instance.
*
* Define correct type hinting like this:
*
* @method WpUserMeta findOneBy()
* @method WpUserMeta[] findBy()
* @method WpUserMeta[] findByQuery()
* @method WpUserMeta[] collectRecords()
*/
class WpUserMeta extends SimpleOrm
{
/**
* Array with table fields
*
* @var array
*/
protected $payload = array(
'umeta_id' => null,
'user_id' => null,
'meta_key' => null,
'meta_value' => null
);
/**
* @var string
*/
protected static $table = 'wp_usermeta';
}
$user_metas = WpUserMeta::getInst()->findBy("meta_key", "description"); // returns array with "WpUserMeta" items
foreach ($user_metas AS $user_meta) {
print_r($user_meta->toArray());
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.