PHP code example of rawebone / ormish

1. Go to this page and download the library: Download rawebone/ormish 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/ */

    

rawebone / ormish example snippets




use Rawebone\Ormish\Table;
use Rawebone\Ormish\Factory;

// We use the factory to build a connection
$factory = new Factory("sqlite::memory:", "", "");
$orm = $factory->build();

// We then "Attach" tables to the container - these are used to provide the
// information we need to connect tables to classes and give options for
// the way SQL should be generated.
$orm->attach(new Table('My\Models\Entity', "data_table"));

// We can then access the database table via it's name, which returns a gateway
// object. This allows us to perform actions such as finding data on our table
// using the options from the Table object.
$gateway = $orm->data_table();
$gateway = $orm->get("data_table");

// We can then find a record
$model = $gateway->find(1);

// Data is accessed by properties
$model->name = "Barry";

// Relationships are accessed by methods
$model->sons(); // Sons[]

// We can save or delete the model directly 
$model->save();
$model->delete();

// Creates a new instance of the Model for our table
// An array of initial data can be passed in this call to populate the model.
$fresh = $gateway->create(array("name" => "john")); 


namespace My\Models;

use Rawebone\Ormish\Entity as BaseEntity;

/**
 * Given that we do not have "real" methods and properties to provide an
 * interface, we can use the annotations (courtesy of ApiGen) for your IDE,
 * if it supports them:
 * 
 * @property int $id
 * @property string $name
 * @property string $complex_name;
 * @method \My\Models\Other sons()
 */
class Entity extends BaseEntity
{
    public function sons()
    {
        return $this->getDatabase()->findWhere("parent_id = ?", $this->id);
    }
}



use Rawebone\Ormish\Transaction;

$tran = new Transaction($orm->getExecutor(), function () use ($entity) {
    $entity->save();
    $entity->delete();
});

$tran->run();