PHP code example of ecsco / ormbase

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

    

ecsco / ormbase example snippets

 php


config **/
$config = \ecsco\ormbase\config\Config::getInstance();

$config->addItem('database', 'server', 'localhost');
$config->addItem('database', 'port', '3306');
$config->addItem('database', 'username', 'my_user');
$config->addItem('database', 'password', 'my_pass');
$config->addItem('database', 'dbname', 'server_v4');
$config->addItem('database', 'databaseclass', '\ecsco\ormbase\database\mysql\Database');
$config->addItem('database', 'debugsql', '1');

/** If memcached available **/
$config->addItem('cache', 'cacheclass', '\ecsco\ormbase\cache\memcached\Cache');
$config->addItem('cache', 'server', '127.0.0.1');
$config->addItem('cache', 'port', '11211');

class TableNameContainer extends \ecsco\ormbase\AbstractContainer {

    const OBJECT_NAME = 'TableClassName';
    const TABLE_NAME  = TableClassName::TABLE_NAME;
    
    protected      $basicFields = [ 'id' ];
     
    use \ecsco\ormbase\traits\SingletonTrait;
     
    public function createNew(  ): ecsco\ormbase\AbstractObject {
        $obj = parent::createNew();
        /** more code **/
        return $obj;
    }
    
    public function getTableNameByID( $id, \ecsco\ormbase\database\FieldSelection $selector = null ) {
        $sql = $this->getDatabase()->select( $selector )->from( self::TABLE_NAME )->where( 'id', '=', (int)$id )->limit( 1 );
        $params = $sql->getQueryParams();
     
        /** make ONE object **/
        $result = $this->makePreparedObject( $sql, self::OBJECT_NAME, ...$params );
     
        return $result;
   }
}

class TableClassName extends \ecsco\ormbase\AbstractObject implements \ecsco\ormbase\CachableInterface {
    
    const TABLE_NAME     = 'TableName';
    
    protected $primaryKeys = [ 'id' ];
    protected $hiddenFields = [ ];
    
    public static function getCacheIdentifier(): string {
        return 'cacheNameForThisEntity';
    }
}

/** Clear first. Just for demonstrating how to get a database-connection **/
$db = \ecsco\ormbase\database\DatabaseAccess::getDatabaseInstance( $config );
$db->query ( $db->delete()->from( \Customer::TABLE_NAME ) );

/** Get the container **/
$cc = TableNameContainer::getInstance();

/** Create a new object based on the table **/
$newEntity = $cc->createNew();
$newEntity->setValue('keyname1', 'value1');
$newEntity->setValue('keyname2', 'value2');
/** .... */
$newEntity->store();

/** Get one object (aka row) with id 1 **/
$entityWithIDOne = $cc->getTableNameByID( 1 ); 
$entityWithIDOne->setValue('key_name_in_table1', 'foobar');
$entityWithIDOne->store();

print_r( $entityWithIDOne->toArray() );

/** ... **/