PHPackages                             kundansingh86/creta - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. kundansingh86/creta

ActiveLibrary[Database &amp; ORM](/categories/database)

kundansingh86/creta
===================

A simple PHP Micro MySQL ORM with Fluent Interface.

00PHP

Since Nov 2Pushed 6y ago1 watchersCompare

[ Source](https://github.com/kundansingh86/Creta)[ Packagist](https://packagist.org/packages/kundansingh86/creta)[ RSS](/packages/kundansingh86-creta/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Creta - PHP Micro ORM with MySQL
================================

[](#creta---php-micro-orm-with-mysql)

A simple PHP Micro MySQL ORM with Fluent Interface

Features
--------

[](#features)

- Easy to use.
- Fluent Design and Interface.
- Required minimum dependency i.e. only PHP &gt;=5.3.0.
- Prevent from SQL Injections.
- Where conditions in hierarchy with AND and OR operators.
- Order by clause with ascending and descending.
- Limit and offset.

Installation (with [Composer](https://getcomposer.org))
-------------------------------------------------------

[](#installation-with-composer)

```
composer require kundansingh86/creta
```

To use the bindings, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):

```
require_once('vendor/autoload.php');
```

Getting Started
---------------

[](#getting-started)

Add namespace and create context.

```
use Creta\MySqlDbContext;

$properties = [
        'host'=>'localhost',
        'username'=>'',
        'password'=>'',
        'database'=>'test'
];

$context = new MySqlDbContext($properties);
```

### Insert Statement

[](#insert-statement)

Insert the data to a table, get the generated inserted ID of the record;

```
$personId = $context->table('person')           // table
                ->insert(['name'=>"Jhon",       // column with name value pair
                        'age'=>25,
                        'salary' => '3000',
                        'department' => 'sales',
                        'position' => 'executive'])
                ->execute();

echo 'Inserted Person ID :: Jhon ', $personId;
```

### Update Statement

[](#update-statement)

Update the data to a table with where condition

```
$context->table('person')     // table
        ->update(['position'=>'assistant manager', 'salary' => '2800'])     // column with name value pair
        ->where(['name' => 'David', 'department'=>'finance']) // AND condition with name value pair
        ->execute();
```

### Delete Statement

[](#delete-statement)

Delete the data from a table with where condition

```
$context->table('person')   // table
        ->delete()
        ->where(['id' => 10])   // condition with name value pair
        ->execute();
```

To delete all records from the table, don't specify the where condition

```
$context->table('person')   //table
        ->delete()
        ->execute();
```

### Select Statement

[](#select-statement)

Select all columns from the table with the where condition

```
$result = $context->table('person') // table
                  ->select()
                  ->where(['id' => 2, 'position' => 'manager'])  // condition with name value pair
                  ->orderBy('name') // order by name in ascending
                  ->execute();

echo '';
print_r($result);
```

Select specific columns from the table

```
$result = $context->table('person') // table
                  ->select(["id", "name", "position"]) // column name array
                  ->orderByDesc('salary', 'position') // order by salary then by position in descending
                  ->limit(2, 1) // limit with 2 records and skip the 1st record
                  ->execute();

echo '';
print_r($result);
```

### Query Output

[](#query-output)

See the sql query output of any statement

```
echo $context->table('person') // table
             ->select()
             ->where(['id' => 2, 'position' => 'manager'])  // condition with name value pair
             ->orderBy('name') // order by name in ascending
             ->query(); // returns the generated sql query
```

### Close Context (Recommended)

[](#close-context-recommended)

Close the context and connection when operations are over

```
$context->close();
```

*Note: A test.sql and test.php files are available in the repo for demo purpose.*

Advance Topics
--------------

[](#advance-topics)

Where conditions formation in various scenarios with AND &amp; OR Cojuctions and other Operators i.e &lt;, &gt;, &lt;= , &gt;=, like

#### Where clause with AND and OR operators

[](#where-clause-with-and-and-or-operators)

###### **Example 1:** SELECT \* FROM person where id = 2 AND position = 'manager'

[](#example-1-select--from-person-where-id--2-and-position--manager)

```
$result = $context->table('person')
                  ->select()
                  ->where(['id' => 2, 'position' => 'manager'])
                  ->execute();
```

###### **Example 2:** SELECT \* FROM person WHERE (position = 'manager' OR (salary &gt;= 2000 AND salary &lt;= 3000))

[](#example-2-select--from-person-where-position--manager-or-salary--2000-and-salary--3000)

```
$result = $context->table('person')
                  ->select()
                  ->where(['position' => 'manager'])
                  ->withOr(['salary >=' => 2000, 'salary ' => 3000])
                  ->withAnd(['position' => ['manager', 'executive']])
                  ->execute();
```

###### **Example 4:** SELECT \* FROM person WHERE position = 'manager' OR position = 'executive'

[](#example-4-select--from-person-where-position--manager-or-position--executive)

```
$result = $context->table('person')
                  ->select()
                  ->whereOr(['position' => ['manager', 'executive']])
                  ->execute();
```

###### **Example 5:** SELECT \* FROM person WHERE (age &gt; 20 AND age &lt; 22) OR (age &gt; 25 AND age &lt; 28) ORDER BY name asc

[](#example-5-select--from-person-where-age--20-and-age--22-or-age--25-and-age--28-order-by-name-asc)

```
$result = $context->table('person')
                  ->select()
                  ->where(['age >' => 20, 'age ' => 25, 'age
