PHP code example of iqomp / model

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

    

iqomp / model example snippets




return [
    'drivers' => [
        'pdo' => 'PDO\\Driver\\Class',
        // ...
    ],
    'models' => [
        'Model\\Class\\Name' => [
            'read' => 'slave',
            'write' => 'master'
        ],
        'Model\\Wildcard\\*' => [
            'read' => 'slave',
            'write' => 'master'
        ]
    ],
    'chains' => [
        'Model\\Class\\Name' => [
            '/field/' => [
                'model' => 'Model\\Other\\Class',
                'self' => 'id',
                'children' => 'wallet_id',
                'type' => 'left'
            ]
        ]
    ]
];



namespace Company\Project;

class Product extends \Iqomp\Model\Model
{
    public static $table = 'product';

    public static $chains = [
        '/field/' => [
            'model' => 'Model\\Other\\Class',
            'self' => 'id',
            'children' => 'wallet_id',
            'type' => 'left'
        ]
    ];
}



return [
    'drivers' => [
        'name' => 'Class'
    ]
];

/**
 * Construct new model object
 * @param array DB connection options
 *  @param string $model Model name
 *  @param string $table Table name
 *  @param array $chains?
 *  @param array $q_field?
 *  @param array $connections
 *    @param array $read List of connection for read
 *    @param array $write List of connection for write
 */
__construct(array $options);

/**
 * Count average value of field
 * @param string $field the field sum total to average
 * @param array $where Where condition
 * @return float Average value of the column
 */
avg(string $field, array $where = []): float;

/**
 * Count total rows in table
 * @param array $where Where condition
 * @return int Total row
 */
count(array $where = []): int;

/**
 * Insert single data to database
 * @param array $row Array column-value pair of data to insert
 * @param bool $ignore Ignore error data already there
 * @return int Last inserted id on success, null otherwise
 */
create(array $row, bool $ignore = false): ?int;

/**
 * Insert many data at once
 * @param array $rows List of array list data to insert
 * @param bool $ignore Ignore exists data if possible
 * @return boolean true on success false otherwise.
 */
createMany(array $rows, bool $ignore = false): bool;

/**
 * Decrease multiple columns with condition
 * @param array $fields List of field-value pair of column to decrease by value
 * @param $where Where condition
 */
dec(array $fields, array $where = []): bool;

/**
 * Escape string to use in raw query
 * @param string $str String to escape
 * @return escaped string
 */
escape(string $str): string;

/**
 * Get single row from table
 * @param array $where Where condition
 * @param array $order Array list of field-direction pair of sort
 * @return object if exists or null
 */
getOne(array $where = [], array $order = ['id' => false]): ?object;

/**
 * Get multiple rows from database
 * @param array $where Where condition
 * @param int $rpp Result per page, default 0 which is all.
 * @param int $page Page number, default 1.
 * @param array $order Array list of field-direction pair of sort
 * @return array list of object or empty array
 */
get(array $where = [], int $rpp = 0, int $page = 1, array $order = ['id' => false]): array;

/**
 * Get connection object
 * @param string $target Connection type target
 * @return resource connection
 */
getConnection(string $target = 'read');

/**
 * Get connection name in config that the model use for $target connection
 * @param string $target Connection type target
 * @return string connection config name
 */
getConnectionName(string $target = 'read'): ?string;

/**
 * Get current connection database name
 * @param string $target Connection type target
 * @return string database name
 */
getDBName(string $target = 'read'): ?string;

/**
 * Get the driver name used for this model
 * @return string driver name
 */
getDriver(): ?string;

/**
 * Get the model name of current model
 * @return string model name
 */
getModel(): string;

/**
 * Get the tabel name that this model handle
 * @return string
 */
getTable(): string;

/**
 * Increase multiple columns with condition
 * @param array $fields List of field-value pair of column to increase by value
 * @param $where Where condition
 */
inc(array $fields, array $where = []): bool;

/**
 * Return last error accured
 * @return string error message or null
 */
lastError(): ?string;

/**
 * Return last id inserted to database
 * @return int last inserted id, or null otherwise
 */
lastId(): ?int;

/**
 * Return the most last executed query
 * @return string if exists, null otherwise
 */
lastQuery(): ?string;

/**
 * Get the maximum value of field from table
 * @param string $field The field to process
 * @param array $where Where condition
 * @return int The max value of field.
 */
max(string $field, array $where = []): int;

/**
 * Get the minimum value of field from table
 * @param string $field THe field to process
 * @param array $where Where condition
 * @return int The smallest value of field.
 */
min(string $field, array $where = []): int;

/**
 * Remove row from table
 * @param array $where Where condition
 * @return boolean true on success, false otherwise.
 */
remove(array $where = []): bool;

/**
 * Update table
 * @param array $fields List of field-value pair of data to update
 * @param array $where Where condition.
 * @return boolean true on success false otherwise.
 */
set(array $fields, array $where = []): bool;

/**
 * Sum table single field.
 * @param string $field The field to sum
 * @param array $where Where conditon.
 * @return int total sum of the field value.
 */
sum(string $field, array $where = []): int;

/**
 * Truncate the table
 * @param string $target Connection target
 */
truncate(string $target = 'write'): bool;



use Company\Project\Model\Product;

$id = Product::create($array);
$product = Product::getOne(['id'=>$id]);

$where = [
    'id'    => 1,
    'name'  => 'User'
];
// `id` = 1 AND `name` = 'User'

$where = [
    'id' => [1,2,3],
    'status' => 1
];
// `id` IN (1,2,3) AND `status` = 1

$where = [
    'id' => ['__op', '!=', 12],
    'status' => 1
];
// `id` != 12 AND `status` = 1

$where = [
    'status' => ['__op', '>', 0]
];
// `status` > 0

$where = [
    'meta' => ['__op', '!=', NULL]
];
// `meta` IS NOT NULL

$where = [
    'status' => ['__op', 'NOT IN', [1,2]]
];
// `status` NOT IN (1,2)

$where = [
    'status' => ['__between', 1, 5]
];
// `status` BETWEEN 1 AND 5

$where = [
    'title' => ['__like', 'name']
];
// `title` LIKE '%name%'

$where = [
    'title' => ['__like', 'name', 'left'],
    // 'title' => ['__like', 'name', 'both']
    // 'title' => ['__like', 'name', 'right']
    // 'title' => ['__like', 'name', 'none']
];
// `title` LIKE '%name'

$where = [
    'title' => ['__like', 'name', null, 'NOT']
];
// `title` NOT LIKE 'name'

$where = [
    'title' => ['__like', ['na1', 'na2', 'na3']]
];
// `title` LIKE '%na1%' OR `title` LIKE '%na2%' OR `title` LIKE '%na3%'

$where = [
    '$and' => [
        [
            'created' => ['__op', '!=', NULL]
        ],
        [
            'created' => ['__op', '>', '2010-02-01']
        ]
    ]
];
// ( ( `created` IS NOT NULL ) AND ( `created` > '2010-02-01' ) )

$where = [
    '$or' => [
        [
            'status' => 1,
            'user' => 2
        ],
        [
            'status' => 2
        ]
    ]
];
// ( ( `status` = 1 AND `user` = 2 ) OR ( `status` = 2 ) )

$where = [
    '?`user`' => 1
];
// `user` = 1

$where = [
    '?JSON_EXTRACT(`price`, \'$.anually\')' => ['__op', '!=', '']
];
// JSON_EXTRACT(`price`, '$.anually') != ''

    // ...
    'rules' => [
        'unique' => [
            'model' => 'Company\\Project\\Model\\Product',
            'field' => 'slug',
            'where' => [ /* ... */ ] // additional where condition
        ]
    ]
    // ...

    // ...
    'rules' => [
        'unique' => [
            'model' => 'Company\\Project\\Model\\Product',
            'field' => [
                // same name of table column and object property
                'slug',

                // object property => table column
                'wallet' => 'wallet_id'

            ],
            'where' => [ /* ... */ ] // additional where condition
        ]
    ]
    // ...

    // ...
    'rules' => [
        'exists' => [
            'model' => 'Company\\Project\\Model\\Product',
            'field' => 'slug',
            'where' => [ /* ... */ ] // additional where condition
        ]
    ]
    // ...

    // ...
    'rules' => [
        'exists-list' => [
            'model' => 'Company\\Project\\Model\\Product',
            'field' => 'slug',
            'where' => [ /* ... */ ] // additional where condition
        ]
    ]
    // ...

$result = Formatter::format('fmt-name', $object, ['user'=>true]);

$result = Formatter::format('fmt-name', $object, ['user' => ['profile'=>true]]);

$result = Formatter::format('fmt-name', $object, [
    'user' => [
        '_where' => [
            'status' => 1
        ]
    ]
]);

    // ...
    'publishers' => [
        'type'      => 'multiple-object',
        'separator' => ',', // 'json'

        'model' => [
            'name' => 'Company\\Project\\Model\\User',
            'field' => 'id'
        ],

        // optional see below
        'field' => [
            'name' => '/field/',
            'type' => '/type/'
        ],

        // optional, see below
        'fields' => [
            ['name' => '/field1/', 'type' => '/type/'],
            ['name' => '/field2/', 'type' => '/type/']
        ],

        // optional, see below
        'format' => '/object-format/'
    ]
    // ...

    // ...
    'tags' => [
        'type' => 'chain',
        'chain' => [
            'model' => [
                'name'  => 'Company\\Project\\Model\\PostTagChain',
                'field' => 'post'
            ],
            'identity' => 'post_tag'
        ],
        'model' => [
            'name'  => 'Company\\Project\\Model\\PostTag',
            'field' => 'id'
        ],

        // optional see below
        'field' => [
            'name' => '/field/',
            'type' => '/type/'
        ],

        // optional, see below
        'fields' => [
            ['name' => '/field1/', 'type' => '/type/'],
            ['name' => '/field2/', 'type' => '/type/']
        ],

        // optional, see below
        'format' => '/object-format/'
    ]
    // ...

    // ...
    'user' => [
        'type' => 'object',
        'model' => [
            'name' => 'Company\\Project\\Model\\User',
            'field' => 'id'
        ],

        // optional see below
        'field' => [
            'name' => '/field/',
            'type' => '/type/'
        ],

        // optional, see below
        'fields' => [
            ['name' => '/field1/', 'type' => '/type/'],
            ['name' => '/field2/', 'type' => '/type/']
        ],

        // optional, see below
        'format' => '/object-format/'
    ]
    // ...

    // ...
    'meta' => [
        'type' => 'object-switch',
        'field' => 'type',
        'cases' => [
            1 => [
                'model' => [
                    'name' => 'Company\\Project\\Model\\User',
                    'field' => 'id'
                ],

                // optional see below
                'field' => [
                    'name' => '/field/',
                    'type' => '/type/'
                ],

                // optional, see below
                'fields' => [
                    ['name' => '/field1/', 'type' => '/type/'],
                    ['name' => '/field2/', 'type' => '/type/']
                ],

                // optional, see below
                'format' => '/object-format/'
            ],
            2 => [
                // ...
            ]
        ]
    ]
    // ...

    // ...
    'content' => [
        'type' => 'partial',
        'model' => [
            'name' => 'Company\\Project\\Model\\User',
            'field' => 'id'
        ],

        // optional see below
        'field' => [
            'name' => '/field/',
            'type' => '/type/'
        ],

        // optional, see below
        'fields' => [
            ['name' => '/field1/', 'type' => '/type/'],
            ['name' => '/field2/', 'type' => '/type/']
        ],

        // optional, see below
        'format' => '/object-format/'
    ]
    // ...
bash
php bin/hyperf.php vendor:publish iqomp/model