PHP code example of pakanhu / myorm

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

    

pakanhu / myorm example snippets


new \paKanhu\MyORM\Database(
    'db_host',
    'db_name',
    'db_username',
    'db_password',
    'db_timeZone'
);

class Product extends \paKanhu\MyORM\Model
{
    protected $id;          // DB Column Name - id (Primary Key)
    protected $sku;         // DB Column Name - sku
    protected $name;        // DB Column Name - name
    protected $price;       // DB Column Name - price
    protected $categoryId;  // DB Column Name - category_id
    protected $createdAt;   // DB Column Name - created_at

    // Table name is mandatory
    protected static $tableName = 'products';

    // Define if primary key in your table is different than "id"
    // protected static $primaryColumn = 'sku';

    /*
    Define if table contains fractional timestamp.
    This is 
    'where' => [[
        'column' => 'categoryId',   // Property name
        'value' => 10,
        // 'condition' => '!=', // MySQL condition (Default is =)
    ]]
]);

// To get 5 new products of category id 10
$products = Product::getAll([
    'where' => [[
        'column' => 'categoryId',
        'value' => 10,
    ]],
    'orderBy' => [
        'column' => 'createdAt',
        'mode' => 'DESC'
    ],
    'limit' => [
        'rowCount' => 5,
        // 'offset' => 10,  // To set offset (Default is 0)
    ]
]);

// To get all products of category id 10 with category details
$products = Product::getByCategoryId(10, [
    'with' => [[
        'column' => 'categoryId',
        'class' => Category::class,
        'property' => 'category',

        // Required when table is not joined with primary key of other table
        // 'foreignColumn' => 'categoryUniqueId',

        // Required for one-to-many relationship (Default value - true)
        // 'isUnique' => false,

        // Complete 'filters' array can be passed as it's in 'getAll' method
        // 'filters' => [],
    ]]
]);
// echo $products[0]->category->name;

// To get all products of category id 10 with only category name
$products = Product::getByCategoryId(10, [
    'with' => [[
        'column' => 'categoryId',
        'class' => Category::class,
        'property' => 'category',
        'filters' => [
            // If 'select' filter is used, then result will be in array
            'select' => ['name']
        ],
    ]]
]);
// echo $products[0]->category['name'];

// To get all product names
$products = Product::getAll([
    'select' => 'name',
    'onlyValues' => true
]);
// echo $products[0];

// To get all products of category id 10, 11, 12 AND name starts with 'Apple'
$products = Product::getAll([
    'where' => [[
        'column' => 'categoryId',
        'condition' => 'IN'
        'value' => [10, 11, 12],
    ], [
        'column' => 'name',
        'condition' => 'LIKE',
        'value' => 'Apple%',
    ]]
]);

// To get all products of category id 10, 11, 12 OR name starts with 'Apple'
$products = Product::getAll([
    'whereOr' => [[
        'column' => 'categoryId',
        'condition' => 'IN'
        'value' => [10, 11, 12],
    ], [
        'column' => 'name',
        'condition' => 'LIKE',
        'value' => 'Apple%',
    ]]
]);

/*
To get all products of category id 10 and name starts with 'Apple' OR
category id 11 and name starts with 'Google'.
SQL Query Representation: WHERE (category_id = 10 AND name LIKE 'Apple%') OR
(category_id = 11 AND name LIKE 'Google%')
*/
$products = Product::getAll([
    'where' => [
        'operator' => 'OR',
        'operands' => [[
            'operator' => 'AND',
            'operands' => [[
                'column' => 'categoryId',
                'value' => 10
            ], [
                'column' => 'name',
                'condition' => 'LIKE',
                'value' => 'Apple%'
            ]]
        ], [
            'operator' => 'AND',
            'operands' => [[
                'column' => 'categoryId',
                'value' => 11
            ], [
                'column' => 'name',
                'condition' => 'LIKE',
                'value' => 'Google%'
            ]]
        ]]
    ]
]);