PHP code example of 4k1r0 / ormega

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

    

4k1r0 / ormega example snippets




// Composer autoloader
as CI;

// Codeigniter style database configuration
$db_data_1 = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'database1',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db_data_2 = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'database2',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

// Creates CI database connectors 
$db1 =& CI\DB($db_data_1);
$db2 =& CI\DB($db_data_2);

// Generator config
$config = array(
    'databases' => array(
        'database1' => array(
            'db' => $db1,
            'filter' => '^(user|profil|application)$',
        ),
        'database2' => array(
            'db' => $db2,
            'filter' => 'business_.*',
        ),
    ),
    'path'      => 'models/',
    'namespace' => 'Ormega'
);

try {
    // Go !
    $Ormegagen = new \Ormega\Generator($config);
    $Ormegagen->run();
}
catch (\InvalidArgumentException $e){
    // ...
}

$config['path'] = 'models/';
$config['namespace'] = 'Ormega';

 if( $oUser->getGenderId() == \Ormega\Database\Enum\Enumgender::MAN ){
    // Do stuff if man
 }

 
        
namespace Ormega\Database\Enum;

class Enumgroup implements \Ormega\EnumInterface {
    
    /** 
     * @var int man gender id
     */
    const MAN = 1;
    
    /** 
     * @var int woman gender id
     */
    const WOMAN = 2;
    
    // ...
}

    /**
     * Get the "Label" associated to an ID
     * @param int $nId
     * @return string
     */
    public static function getLabel( $nId ){ 
        // ... 
    }
    
    /**
     * Get the "Constant" associated to an ID
     * @param int $nId
     * @return string
     * @author Ormegagenerator_lib
     */
    public static function getConstant( $nId ){ 
        // ... 
    }
    
    **
     * Get all the constants in a array form
     * @return array
     * @author Ormegagenerator_lib
     */
    public static function getArray(){ 
        return array(
            "QUALITE" => array(
                "id" => "1",
                "label" => "man gender id",
                "constant" => "MAN",
            ),
            "MANAGER" => array(
                "id" => "2",
                "label" => "woman gender id",
                "constant" => "WOMAN",
            ),
        );
    }
    
    **
     * Get an ID from a string constant
     * @param string $sConstant
     * @return int
     * @author Ormegagenerator_lib
     */
    public static function getId( $sConstant ){ 
        // ... 
    }



// Composer autoloader
as CI;

// Codeigniter style database configuration
$db_data_1 = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'database1',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db_data_2 = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'database2',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

// Creates CI database connectors 
$db1 =& CI\DB($db_data_1);
$db2 =& CI\DB($db_data_2);

try {
    
    // Orm config. Index must be the same as declared in the generator
    $aInit = array(
        'database1' => $db1,
        'database2' => $db2,
    );

    

$oCareneeds = new \Ormega\Entity\User();
$oCareneeds->setEmail('[email protected]')
           ->setAge(20)
           ->setEmailPublic(true)
           ->save();

$oUserCollection = \Ormega\Query\User::create()
    ->filterByAge(20, \Ormega\Orm::OPERATOR_LOWER_THAN)
    ->filterById($aIds, \Ormega\Orm::OPERATOR_IN)
    ->filterByIsActive(true) // EQUALS as default second argument
    ->orderByDateinsert('DESC')
    ->find();
    
$oUserEntity = \Ormega\Query\User::create()
    ->filterById( $nUserId )
    ->findOne();
   
// Test results
if( $oUserCollection->isEmpty() ){
    // No results   
}

// browse each result
foreach( $oUserCollection as $oUserEntity ){
    /**
     * @var \Ormega\Entity\User $oUserEntity
     */
}

// Get all primary key (ex : to use it within a `WHERE field IN()` sql statement)
$aIds = $oUserCollection->getArrayKeys()


$oUserEntity = new Ormega\Database\Entity\User();
$oProfilEntity = new Ormega\Database\Entity\Profil();

$oProfilEntity->setUser( $oUserEntity );
$oProfilEntity->setFkUserId( $oUserEntity->getId() );  // this 2 lines do the same stuff

// Same way for getters :
$oProfilEntity = new Ormega\Database\Entity\Profil();
$oUserEntity = $oProfilEntity->getUser();

 
        
namespace Ormega\Database\Entity\Base;

class Profil implements \Ormega\EntityInterface {
     
    /**
     * @var int $fk_user_id Null:NO Maxlenght:10
     */
    protected $fk_user_id;
    
    /**
     * @var Ormega\Database\Entity\User $user Null:NO
     */
    protected $user;
    
    // ...
    
    /**
     * @param int $fk_user_id Maxlenght:11
     * @throw \InvalidArgumentException
     */
    public function setFkUserId( $fk_user_id )
    {
        if( !is_int( $fk_user_id ) ) {
            throw new \InvalidArgumentException("Invalid parameter for \"".__METHOD__."\" : (int) expected ; \"$fk_user_id\" (".gettype($fk_user_id).") provided");
         }
            
        $this->fk_user_id = $fk_user_id;
        $this->modified(true);
        
        return $this;
    }
    
    /**
     * @param \Ormega\Database\Entity\User $user
     */
    public function setComment(\Ormega\Database\Entity\User $user)
    {
        $this->user = $user;
        $this->fk_user_id = $user->getId();
        $this->modified(true);
        
        return $this;
    }
}