Menu

## Porm
in package
Uses TableLevelQueryTrait, AggregateTrait

Porm.php : 10

### Table of Contents

#### Properties

$database : mixed
  The CDatabase object to use
$alias : string|null
$allowFilterOnly : bool
$columns : string|array<string|int, mixed>|null
$preventHas : bool
$preventRaw : true
$resultSet : mixed
$table : mixed
  The CDatabase table to use. This is for interoperability with other versions of Porm
$where : array<string|int, mixed>

#### Methods

__construct() : mixed
asJson() : bool|string
asObject() : mixed
avg() : string|null
columns() : static
  This defines the table column names to return from the CDatabase
count() : int|null
createOrUpdate() : object|array<string|int, mixed>
  Acronym for saveOrUpdate
deleteAll() : PDOStatement|null
  This deletes all items that match the where clause
deleteById() : PDOStatement|null
  This is under the hood similar to deleteOne but it is more explicit
filter() : Builder
  This switches the query to filter mode. It is useful for conditional querying.
first() : object|array<string|int, mixed>|null
  This grabs the first [n] items from the CDatabase based on the pkField given
get() : object|array<string|int, mixed>|null
  Fetches a single item from the CDatabase.
getAlias() : string|null
getDatabase() : Piql|null
getOrThrow() : object|array<string|int, mixed>|null
  Get a resource or throw an exception if it does not exist
has() : bool
  This checks if the table has a record that matches the where clause
info() : array<string|int, mixed>
  Returns the details of the current db connection
inTransaction() : void
  Using transactions. This is a wrapper for the action method in the Core class.
join() : object|null
  Opens the portal to the joins builder. Once you call this, you can call the join methods
last() : object|array<string|int, mixed>|null
  Grab the last item from the CDatabase based on the pkField clause
lastQuery() : string|null
  Logs the last query that was run
lastSaved() : string|null
  Returns the last saved id
max() : string|null
min() : string|null
random() : array<string|int, mixed>|mixed|object
  Fetches random n items from the table, default to 1
raw() : mixed
  This assists to perform raw sql queries
save() : object
  Saves and returns the saved item as an object
saveAll() : array<string|int, mixed>|PDOStatement|null
  Save multiple items in the database.
saveOrUpdate() : object|array<string|int, mixed>
  Create a new item or update an existing item Supports both single and multiple items
setAlias() : void
setDatabase() : void
setTable() : void
sum() : string|null
update() : PDOStatement|null
checkFilterMode() : void
  This prevents the use of non-filtering methods in filter mode.

### Properties

#### $database

Porm.php : 16

The CDatabase object to use

`public mixed $database`

#### $alias

Porm.php : 27
`private string|null $alias`

The alias to use, will defualt to the table name provided.

#### $allowFilterOnly

Porm.php : 37
`private bool $allowFilterOnly = false`

Lock out the use of any other method other than filter

#### $columns

Porm.php : 47
`private string|array<string|int, mixed>|null $columns = '*'`

The columns to select

#### $preventHas

Porm.php : 32
`private bool $preventHas = false`

Lock out the use of filter

#### $preventRaw

Porm.php : 51
`private true $preventRaw = false`

Lock out the use of raw queries

#### $resultSet

Porm.php : 42
`private mixed $resultSet`

The result set to call asObject and asJson on.

#### $table

Porm.php : 22

The CDatabase table to use. This is for interoperability with other versions of Porm

`private mixed $table`

#### $where

Porm.php : 56
`private array<string|int, mixed> $where = []`

The columns to select

### Methods

#### __construct()

Porm.php : 60
`public __construct(Connection $connection) : mixed`

##### Parameters

$connection : Connection

#### asJson()

TableLevelQueryTrait.php : 178
`public asJson() : bool|string`

##### Tags

throws
  Exception

##### Return values

bool|string

#### asObject()

TableLevelQueryTrait.php : 188
`public asObject() : mixed`

##### Tags

throws
  Exception

#### avg()

AggregateTrait.php : 57
`public avg(string $column, array<string|int, mixed>|null $where) : string|null`

##### Parameters

$column : string
$where : array<string|int, mixed>|null

##### Tags

see
  Porm::avg()

##### Return values

string|null

#### columns()

TableLevelQueryTrait.php : 355

This defines the table column names to return from the CDatabase

`public columns([string|array<string|int, mixed> $columns = "*" ]) : static`

If you're in join mode, then all ambigous columns should define the table as an alias

##### Parameters

$columns : string|array<string|int, mixed> = "*"

  The columns to select defaults to * for all.

##### Tags

throws
  Exception
example

        $res1 = Porm::from('users')->columns('first_name')->get(1); // fetches the first name of the user with id 1
        $res2 = Porm::from('users')->columns(['first_name', 'last_name'])->get(1); // fetches the first name and last name of the user with id 1
        $res3 = Porm::from('users')->columns(['first_name', 'last_name'])->filter(['last_name' => 'Pionia'])->all(); // fetches the first name and last name of all users with last name Pionia

      // In joins

       $res4 = Porm::from("users", "u")
      ->columns(["u.id", "role_name", "role.created_at"])
      ->join()->left("role", [u.role_id => id])
      ->all();

##### Return values

static

#### count()

AggregateTrait.php : 31
`public count([string|null $column = null ][, array<string|int, mixed>|null $where = null ]) : int|null`

##### Parameters

$column : string|null = null
$where : array<string|int, mixed>|null = null

##### Tags

see
  Porm::count()

##### Return values

int|null

#### createOrUpdate()

TableLevelQueryTrait.php : 295

Acronym for saveOrUpdate

`public createOrUpdate(array<string|int, mixed>|Arrayable $data[, string $pkField = 'id' ]) : object|array<string|int, mixed>`

##### Parameters

$data : array<string|int, mixed>|Arrayable
$pkField : string = 'id'

##### Tags

see

  $this->saveOrUpdate()

throws
  Exception
since
  2.0.3+

##### Return values

object|array<string|int, mixed>

#### deleteAll()

TableLevelQueryTrait.php : 416

This deletes all items that match the where clause

`public deleteAll(array<string|int, mixed> $where) : PDOStatement|null`

##### Parameters

$where : array<string|int, mixed>

##### Tags

throws
  Exception
example

       $res1 = Porm::from('users')->deleteAll(['name' => 'John']); // deletes all users with name John
       $res2 = Porm::from('users')->deleteAll(['last_name' => 'Pionia', 'first_name'=>'Framework']); // deletes all users with last name Pionia and first_name as Framework

##### Return values

PDOStatement|null

#### deleteById()

TableLevelQueryTrait.php : 450

This is under the hood similar to deleteOne but it is more explicit

`public deleteById(string|int $id[, string|null $idField = 'id' ]) : PDOStatement|null`

##### Parameters

$id : string|int
$idField : string|null = 'id'

##### Tags

throws
  Exception

##### Return values

PDOStatement|null

#### filter()

TableLevelQueryTrait.php : 324

This switches the query to filter mode. It is useful for conditional querying.

`public filter([array<string|int, mixed>|null $where = [] ]) : Builder`

##### Parameters

$where : array<string|int, mixed>|null = []

  The where clause to use

##### Tags

throws
  Exception
example

       $res1 = Porm::from('users')->filter(['id' => 1])->get(); // fetches a user with id 1
       $res2 = Porm::from('users')->filter(['last_name' => 'Pionia', 'first_name'=>'Framework'])->all(); // fetches all users with last name Pionia and first_name as Framework
       $res2 = Porm::from('users')->filter(['last_name' => 'Pionia'])->limit(1)->startAt(2); // fetches a user with last name Pionia and first_name as Framework

##### Return values

Builder

#### first()

TableLevelQueryTrait.php : 484

This grabs the first [n] items from the CDatabase based on the pkField given

`public first([int|null $size = 1 ][, array<string|int, mixed>|null $where = [] ][, string $pkField = 'id' ]) : object|array<string|int, mixed>|null`

##### Parameters

$size : int|null = 1

  The number of items to fetch

$where : array<string|int, mixed>|null = []

  The where clause to use

$pkField : string = 'id'

  The primary key field to use

##### Tags

throws
  Exception

##### Return values

object|array<string|int, mixed>|null

#### get()

TableLevelQueryTrait.php : 218

Fetches a single item from the CDatabase.

`public get([int|array<string|int, mixed>|string|null $where = null ][, string|null $idField = 'id' ]) : object|array<string|int, mixed>|null`

If the where clause is not passed, it fetches the last item in the table. If the where clause is an integer, it fetches the item with the id. If the where clause is an array, it fetches the item that matches the where clause. If the where clause is null, it fetches the last item in the table.

##### Parameters

$where : int|array<string|int, mixed>|string|null = null
$idField : string|null = 'id'

  defaults to id, pass this if you want to use a different field as the id other than id

##### Tags

throws
  Exception
example

         $res1 = Porm::from('users')->get(1); // fetches a user with id 1
         $res2 = Porm::from('users')->get(['id' => 1]); // fetches a user with id 1
         $res3 = Porm::from('users')->get(['last_name' => 'Pionia', 'first_name'=>'Framework']); // fetches a user with last name Pionia and first_name as Framework
        $res4 = Porm::from('users')->get(); // fetches the latest user in the table

##### Return values

object|array<string|int, mixed>|null

#### getAlias()

Porm.php : 158
`public getAlias() : string|null`

##### Return values

string|null

#### getDatabase()

Porm.php : 93
`public getDatabase() : Piql|null`

##### Return values

Piql|null

#### getOrThrow()

TableLevelQueryTrait.php : 245

Get a resource or throw an exception if it does not exist

`public getOrThrow([int|array<string|int, mixed>|string|null $where = null ][, string $message = 'Item not found' ][, string|null $idField = 'id' ]) : object|array<string|int, mixed>|null`

##### Parameters

$where : int|array<string|int, mixed>|string|null = null
$message : string = 'Item not found'
$idField : string|null = 'id'

##### Tags

version
  2.0.3+
since
  2.0.3
  - 2024-08-09
throws
  Exception

##### Return values

object|array<string|int, mixed>|null

#### has()

TableLevelQueryTrait.php : 46

This checks if the table has a record that matches the where clause

`public has(string|array<string|int, mixed>|int|null $where[, string|null $pkField = 'id' ]) : bool`

##### Parameters

$where : string|array<string|int, mixed>|int|null
$pkField : string|null = 'id'

##### Tags

throws
  Exception
example

           $res1 = Porm::from('users')->has(1); // with integer where clause that defaults to id = 1
           $res2 = Porm::from('users')->has(['id' => 1]); // with array where clause

##### Return values

bool

#### info()

Porm.php : 129

Returns the details of the current db connection

`public info() : array<string|int, mixed>`

##### Return values

array<string|int, mixed>

#### inTransaction()

Porm.php : 117

Using transactions. This is a wrapper for the action method in the Core class.

`public inTransaction(callable $callback) : void`

To access data outside the transaction, Create a result variable and refer to the transaction callback with the keyword `use`, and you can get data back after when you assign it from inside.

##### Parameters

$callback : callable

  The callback to run. It should return a void.

##### Tags

example

           $row = null;
           Table::from('qa_criteria')
                 ->inTransaction(function (Table $instance) use (&$row) {
                       $row = $instance->save([
                           'name' => 'Status 3',
                           'description' => 'Must be single 4',
                           'best_of_total' => 6,
                       ]);
                   });

           var_dump($row);

throws
  Exception

#### join()

TableLevelQueryTrait.php : 467

Opens the portal to the joins builder. Once you call this, you can call the join methods

`public join([array<string|int, mixed>|null $where = null ]) : object|null`

##### Parameters

$where : array<string|int, mixed>|null = null

##### Tags

example

      $res4 = Porm::from("users", "u")
      ->columns(["u.id", "role_name", "role.created_at"])
      ->join()->left("role", [u.role_id => id])
      ->all();

##### Return values

object|null

#### last()

TableLevelQueryTrait.php : 513

Grab the last item from the CDatabase based on the pkField clause

`public last([int|null $size = 1 ][, array<string|int, mixed>|null $where = [] ][, string $pkField = 'id' ]) : object|array<string|int, mixed>|null`

##### Parameters

$size : int|null = 1

  The number of items to fetch

$where : array<string|int, mixed>|null = []

  The where clause to use

$pkField : string = 'id'

  The primary key field to use

##### Tags

throws
  Exception

##### Return values

object|array<string|int, mixed>|null

#### lastQuery()

Porm.php : 85

Logs the last query that was run

`public lastQuery() : string|null`

##### Return values

string|null —

The last query that was run

#### lastSaved()

Porm.php : 138

Returns the last saved id

`public lastSaved() : string|null`

##### Return values

string|null

#### max()

AggregateTrait.php : 69
`public max(string $column, array<string|int, mixed>|null $where) : string|null`

##### Parameters

$column : string
$where : array<string|int, mixed>|null

##### Tags

see
  Porm::max()

##### Return values

string|null

#### min()

AggregateTrait.php : 81
`public min(string $column, array<string|int, mixed>|null $where) : string|null`

##### Parameters

$column : string
$where : array<string|int, mixed>|null

##### Tags

see
  Porm::min()

##### Return values

string|null

#### random()

TableLevelQueryTrait.php : 76

Fetches random n items from the table, default to 1

`public random([int|null $limit = 1 ][, array<string|int, mixed>|null $where = null ]) : array<string|int, mixed>|mixed|object`

##### Parameters

$limit : int|null = 1
$where : array<string|int, mixed>|null = null

##### Tags

example

          $res1 = Porm::from('users')->random(); // fetches a random user
          $res2 = Porm::from('users')->random(5); // fetches 5 random users
          $res3 = Porm::from('users')->random(5, ['last_name' => 'Pionia']); // fetches 5 random users with last name Pionia

throws
  Exception

##### Return values

array<string|int, mixed>|mixed|object

#### raw()

Porm.php : 69

This assists to perform raw sql queries

`public raw(string $query[, array<string|int, mixed>|null $params = [] ][, string|null $using = 'db' ]) : mixed`

##### Parameters

$query : string
$params : array<string|int, mixed>|null = []
$using : string|null = 'db'

##### Tags

throws
  Exception

#### save()

TableLevelQueryTrait.php : 115

Saves and returns the saved item as an object

`public save(array<string|int, mixed> $data) : object`

##### Parameters

$data : array<string|int, mixed>

  The data to save. Must be an associative array

##### Tags

throws
  Exception
example

         $res = Porm::from('users')->save(['first_name' => 'John', 'last_name' => 'Doe']);
         echo $res->id;

##### Return values

object —

The saved object

#### saveAll()

TableLevelQueryTrait.php : 136

Save multiple items in the database.

`public saveAll(array<string|int, mixed> $data[, bool $returning = true ]) : array<string|int, mixed>|PDOStatement|null`

##### Parameters

$data : array<string|int, mixed>

  The data to save. Must be an associative array

$returning : bool = true

  If true, it returns the resultset containing the saved items

##### Tags

throws
  Exception
since
  2.0.3+
  - 2024-08-09
example

       $res = table('users')->saveAll([['first_name' => 'John', 'last_name' => 'Doe'], ['first_name' => 'Jane', 'last_name' => 'Doe']]);
      $r = table('users')->saveAll([['first_name' => 'John', 'last_name' => 'Doe'], ['first_name' => 'Jane', 'last_name' => 'Doe']], true);

##### Return values

array<string|int, mixed>|PDOStatement|null

#### saveOrUpdate()

TableLevelQueryTrait.php : 260

Create a new item or update an existing item Supports both single and multiple items

`public saveOrUpdate(array<string|int, mixed>|Arrayable $data[, string $pkField = 'id' ]) : object|array<string|int, mixed>`

##### Parameters

$data : array<string|int, mixed>|Arrayable
$pkField : string = 'id'

##### Tags

throws
  Exception
since
  2.0.3+

##### Return values

object|array<string|int, mixed>

#### setAlias()

Porm.php : 153
`public setAlias(string|null $alias) : void`

##### Parameters

$alias : string|null

#### setDatabase()

Porm.php : 143
`public setDatabase(Piql $database) : void`

##### Parameters

$database : Piql

#### setTable()

Porm.php : 148
`public setTable(string|null $table) : void`

##### Parameters

$table : string|null

#### sum()

AggregateTrait.php : 45
`public sum(string $column, array<string|int, mixed>|null $where) : string|null`

##### Parameters

$column : string
$where : array<string|int, mixed>|null

##### Tags

see
  Porm::sum()

##### Return values

string|null

#### update()

TableLevelQueryTrait.php : 164
`public update(array<string|int, mixed> $data, array<string|int, mixed>|int|string $where[, string|null $idField = 'id' ]) : PDOStatement|null`

##### Parameters

$data : array<string|int, mixed>
$where : array<string|int, mixed>|int|string
$idField : string|null = 'id'

##### Tags

throws
  Exception

##### Return values

PDOStatement|null

#### checkFilterMode()

TableLevelQueryTrait.php : 435

This prevents the use of non-filtering methods in filter mode.

`private checkFilterMode([string $msg = 'Query is in filter mode, you cannot use this method in filter mode' ]) : void`

Case here is like calling get() on join() yet join() return no resultset yet.

##### Parameters

$msg : string = 'Query is in filter mode, you cannot use this method in filter mode'

  The message to throw

  This is primarily used internally for the purpose.

      $this->checkFilterMode("You cannot delete at this point in the query, check the usage of the `delete()` method in the query builder for ".$this->table);

##### Tags

throws
  Exception

## Search results