PHP code example of reneschmidt / simpleorm

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/ */

    

reneschmidt / simpleorm example snippets


/**
 * Sample Model instance.
 *
 * Define correct type hinting like this:
 *
 * @method Sample findOneBy($field, $value, $fetchMode = \PDO::FETCH_OBJ)
 * @method Sample[] findBy($field, $value, $fetchMode = \PDO::FETCH_OBJ)
 * @method Sample[] findByQuery($query, array $values, $fetchMode = \PDO::FETCH_OBJ)
 * @method Sample[] collectRecords(\PDOStatement $sth, $fetchMode = \PDO::FETCH_OBJ)
 */
class Sample extends SimpleOrm
{
 /**
  * Array with table fields
  *
  * @var array
  */
 protected $payload = array(
   "id" => null, // first field is primary key
   "some_name" => null,
   "bitmask" => null
 );

 /**
  * @var string
  */
 protected static $table = 'sample';
}

$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());
}