PHP code example of tyrion / simple-pdo-orm

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

    

tyrion / simple-pdo-orm example snippets


$dbWrapper = new DbWrapper($config);                                // Create connection from config data

$dbStatement = new DbStatement($dbWrapper->getConnection());        // get DB manager that run queries

// SELECT all entities from `car` table
$cars = $dbStatement
    ->setRowItemInstance(new Car())                                 // Set Class we want to generate using reflection form entities
    ->setQuery("SELECT * FROM {$dbStatement->getModelTableName()}") // set sql Query
    ->findResult();                                                 // return DbTableRowIterator

while ($cars->hasNextItem()) {                                      // Iterate selected entities
    $car = $cars->getNextItem();
    echo $car->getModel() . "<br/>";
}

// SELECT specific car entity
$car = $dbStatement
    ->setRowItemInstance(new Car())
    ->setQuery("SELECT * FROM {$dbStatement->getModelTableName()} WHERE id = ?")
    ->setArguments(array(1))
    ->findOne();
    
// SELECT unspecified model item: ClearPDOItem
$data = $dbStatement
    ->setQuery("SELECT * FROM `car` WHERE `brand` LIKE '%?%'")
    ->setArguments(array("Fiat"))
    ->findResult()

class Car implements DbTableRowItem
{
    private const DB_TABLE = "car";
    
    private $id;
    private $brand;
    private $year_of_production;
    private $price;
    private $model;
    /**
     * Support for property annotation for ignore unrelated columns
     * @tyrion-orm-ignore
    */
    private $test;

    public function getId():int
    {
        return $this->id;
    }
    
    public function getBrand():String
    {
        return $this->brand;
    }
    
    ...
    // No setter necessary, only if you need to for UPDATE / INSERT
    
}
sql
SELECT * FROM car