PHP code example of hegentopf / easy-orm

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

    

hegentopf / easy-orm example snippets


use Hegentopf\EasyOrm\connection\ConnectionManager;
use Hegentopf\EasyOrm\connection\MySQLConnection;

// Set up the database connection, adjust parameters as needed.
// Use .env or config files in production
$default = new MySQLConnection( 'dbName', 'user', 'passwd', 'localhost', 3306 );
ConnectionManager::setConnection( $default );

// For multiple connections, you can set and get connections by name
$replication = new MySQLConnection( 'dbName', 'user', 'passwd', 'localhost', 3307 );
ConnectionManager::setConnection( $replication, 'replication' );


use Hegentopf\EasyOrm\modelCreator\DbModelCreator;

$dbModelCreator = new DbModelCreator();
$dbModelCreator
    ->setNamespace( 'App\\dbModels' )
    ->setPath( __DIR__ . '/../src/dbModels' )
    ->createAllDbModels( true ); // true = override existing models

use App\dbModels\test\ProductModel;

// Create a new model
$productModel = new ProductModel();
$productModel->setName( 'Screwdriver' )->setPrice( 15.40 )->save();

// Fetch all models
$productModel = ProductModel::getQueryBuilder()->get();

// Fetch a single model by primary key
$productModel = ProductModel::fetchById( 1 );

// Update a model
$productModel->setName( 'Nail' )->setPrice( 0.22 )->save();

// Delete a model
$productModel->delete();

use App\dbModels\test\ProductModel;

$productModels = ProductModel::getQueryBuilder()
    ->select( ProductModel::name(), ProductModel::price() )
    ->limit( 10 )
    ->get();

use App\dbModels\test\ProductModel;
use Hegentopf\EasyOrm\queryBuilder\OrderBy;

$products = ProductModel::getQueryBuilder()
                        ->select( 
                                ProductModel::name(),
                                ProductModel::price(),
                                ProductModel::timestamp_created()
                            )
                        ->where( ProductModel::name(), '=', 'Screwdriver' )
                        ->groupBy( ProductModel::name() )
                        ->orderBy( ProductModel::name(), OrderBy::DESC )
                        ->limit( 3 )
                        ->get();

use App\dbModels\test\OrderModel;
use App\dbModels\test\ProductModel;

$orders = OrderModel::getQueryBuilder()
                    ->select(
                        OrderModel::id(),
                        OrderModel::order_date(),
                        ProductModel::name(),
                        ProductModel::price()
                    )
                    ->leftJoin(
                        ProductModel::getTable(),
                        ProductModel::order_id(),
                        OrderModel::id()
                    )
                    ->orderBy( OrderModel::order_date() )
                    ->get();

use App\dbModels\shop\OrderModel;
use App\dbModels\shop\ProductModel;

$subQuery = ProductModel::getQueryBuilder()
                        ->select( ProductModel::order_id() )
                        ->where( ProductModel::price(), '>', 100 )
                        ->groupBy( ProductModel::order_id() );

$orders = OrderModel::getQueryBuilder()
                    ->select(
                        OrderModel::id(),
                        OrderModel::order_date()
                    )
                    ->whereIn( OrderModel::id(), $subQuery )
                    ->get();