PHP code example of bertugfahriozer / ci4commonmodel

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

    

bertugfahriozer / ci4commonmodel example snippets


    use ci4commonmodel\Models\CommonModel;

    class ExampleController extends BaseController
    {
        protected $commonModel;

        public function __construct()
        {
            $this->commonModel = new CommonModel();
        }
    }
    

// Simple usage
$users = $this->commonModel->lists('users', '*', ['status' => 1], 'id DESC', 10);

// Advanced usage with JOIN and LIKE
$joins = [
    ['table' => 'roles', 'cond' => 'users.role_id = roles.id', 'type' => 'left']
];
$like = ['name' => 'John'];
$users = $this->commonModel->lists('users', 'users.*, roles.name as role', ['status' => 1], 'users.id DESC', 10, 0, $like, [], $joins);

$data = [
    'name' => 'John Doe',
    'email' => '[email protected]',
    'status' => 1
];

$insertId = $this->commonModel->create('users', $data);

$data = [
    ['name' => 'Alice', 'email' => '[email protected]'],
    ['name' => 'Bob', 'email' => '[email protected]']
];

$this->commonModel->createMany('users', $data);

$data = ['status' => 2];
$where = ['id' => 1];

$this->commonModel->edit('users', $data, $where);

$where = ['id' => 1];
$this->commonModel->remove('users', $where);

$where = ['status' => 1];
$count = $this->commonModel->count('users', $where);

$where = ['id' => 1];
$isExist = $this->commonModel->isHave('users', $where);

$like = ['name' => 'John'];
$where = ['status' => 1];

$results = $this->commonModel->research('users', $like, '*', $where);

$tables = $this->commonModel->getTableList();

$fields = [
    'id' => [
        'type' => 'INT',
        'constraint' => 5,
        'unsigned' => true,
        'auto_increment' => true,
    ],
    'title' => [
        'type' => 'VARCHAR',
        'constraint' => '100',
        'unique' => true,
    ]
];
$this->commonModel->newTable('blog', $fields);

$this->commonModel->removeTable('blog');

$fields = [
    'preferences' => ['type' => 'TEXT', 'after' => 'email'],
];
$this->commonModel->addColumnToTable('users', $fields);

$this->commonModel->removeColumnFromTable('users', ['preferences']);

$this->commonModel->updateTableName('old_table', 'new_table');

$fields = [
    'old_name' => [
        'name' => 'new_name',
        'type' => 'TEXT',
        'null' => false,
    ],
];
$this->commonModel->modifyColumnInfos('users', $fields);

$this->commonModel->emptyTableDatas('users');

$fields = $this->commonModel->getTableFields('users');

$this->commonModel->newDatabase('new_db');

$this->commonModel->removeDatabase('old_db');

$this->commonModel->drpPrimaryKey('users');

$this->commonModel->drpKey('users', 'my_key_name');

$this->commonModel->drpForeignKey('orders', 'fk_orders_users');