Database
Last updated
Last updated
Butterfly uses multi-layer database management. It's possible to use multiple databases at the same time. You can configure multiple databases and use it. Butterfly uses MySQL as the primary database. All Butterfly related data is stored in MySQL but you can use the following database drivers:
MySQL 5.6+
ElasticSearch 7+
MongoDB
Redis
MSSQL
Oracle Database
When you check MySQL and other database implementations you will see that it's written to make developer comfortable whether you are using MySQL or ElasticSearch. We have built a system with same behaviours independent from which driver you use.
Why you should mess with Elastic Search complex JSON Queries if there is a better solution. You don't need anymore. Just give a shot to Butterfly implementations.
[!TIP] Butterfly Database Layer syntax is inspired by
Butterfly MySQL Implementation syntax is inspired from Laravel. Core aim of implementation is to maintain readability besides having performant queries. You can generate performant queries easily by using built-in functions.
The database configurations are stored in app/config/database.php
. Configurations can be customized by domain name with subfolders. Example configuration:
In this release, we've introduced the capability to define settings either globally or on a per-connection basis within your configuration files. This flexibility allows for more precise control over database interactions.
Global Configuration
To define global settings, you can now add configurations in the app/Config/database.php
file. For instance, to globally enable the \PDO::ATTR_STRINGIFY_FETCHES
option (which converts numeric values to strings when fetching), add the following entry:
This will apply the specified options to all database connections unless overridden on a connection-specific basis.
Connection-Specific Configuration
For configuring settings specific to a particular database connection, modify the connection array in your configuration file. Here's an example for the 'default' connection:
In this example, the \PDO::ATTR_STRINGIFY_FETCHES
option is specifically set for the 'default' database connection. If this option is also set globally, the connection-specific setting will take precedence.
Butterfly uses slave database if it's defined. If not, it uses default database for the SELECT operations.
Database client can be reached using db()
helper. You can also reach defined databases using the database alias as the first parameter.
Following function call will return a database client connected to database external defined in configuration.
You can begin, rollback or commit a transaction.
will run the query:
and return all results as associative array.
Specifying Columns
You may want to return specific columns:
Column List
Column List with Alias
You can also use aliases using following example:
will run the following query:
Where
You can write where clauses in many ways. Examples are the following:
will run the query:
and return one row as associative array.
will run query:
You can use parameter binding, and bind parameters:
[!WARNING] You don't need bind function for shorthand where clauses. You can use directly bind parameter as second parameter to where clauses. For example:
->where('id', 5)
will bind parameters automatically. Don't usebind
function for non-complex where operations.
will run the query:
You can bind parameters using question marks (?)
will run:
[!DANGER] Question mark style binding, doesn't work with associative arrays.
Following code will generate error:
whereIn
You can use arrays with where clauses:
will run the query:
whereNotIn
You can use arrays with where clauses:
will run the query:
whereNull
By using whereNull, you can easily filter only null values:
will run the query:
whereNotNull
will run the query:
Nested Clause
Nested SQL Queries can be generated using callback functions.
As you can see below, queries inside of the function will be evaluated seperately inside of braces and it will run:
Nested Clause with Multiple Depth
Nested SQL Queries can be generated using multiple callback functions.
As you can see below, queries inside of the function will be evaluated seperately inside of braces and it will run:
orWhere
You can also use operators in where clauses. If you pass where clauses as an array, all clauses in the array will be joined using AND
operator.
will run:
orWhereIn
will run the query:
orWhereNull
will run the query:
orWhereNotNull
will run the query:
Distinct
will run the queris:
Between
will run:
Not Between
will run:
OR Between
will run:
Not Between
will run:
Join
You can join tables:
Inner Join
will run the query:
[!WARNING] Join function uses INNER JOIN Statement
Left Join
You can left join tables:
will run the query:
Right Join
You can right join tables:
will run the query:
Use Index
will run the query:
Force Index
will run the query:
Order By
You can order by column ascending or descending order:
Group By
You can group by column:
For multiple columns, you can use comma seperator:
Having
will run the following query:
Skip (Offset)
You can add an offset to the result using skip
function
will run the query:
[!TIP] You can use skip function with limit or pagination functions. It will basically skip
X
results and limit or paginate after skipped records.
Limit
Results can be limited using limit function. Only the first number of rows will be returned.
Will run the query:
And will return first 10 rows from users
table.
Pagination
You can define paginate results by using paginate
function.
$limit
Sets the limit per page
20
$page_no
Sets the current page
1
Will run the following query:
And will return the results from 2nd page starting from 6.
to 10.
record.
Find
You can use find
function to return one row using identifier
will return one row with id = 1
Die
For debugging purposes, you can use die
function to output the SQL Query that will run.
will output the query that will be executed.
[!NOTE]
die
function will not run the query, just outputs it and terminates the script.
Returning Functions
Returning functions are used to execute the query and return the processed result.
Let's think of a virtual users
table with the following records:
1
John Doe
waiting
2
Jane Doe
waiting
3
Jack Hamel
active
get
Will return all rows as associative array.
will return
first
Will just return the first row as associative array
will return
count
Will return the number of rows for the query without any Group By statement.
will return:
(int) 3
one
Will return only one column value
will return the name column value of the first row.
John Doe
column
Will return the values of specific column as an array list
will return names as an array list.
keyToValue
Will return result indexed by key_column. Value will be the row as the associative array.
will return:
When you call keyToValue function with two parameters, it will use the column value
will return result indexed by key_column. Value will be value of the specified column:
keyToValues
when key_column is not unique, you can use this function to group results by key_column (e.g. status). Value will be the row as the associative array.
will return:
When key_column is not unique, you can use this function to group results by key_column (e.g. status). Value will be value of the specified column
will return result indexed by key_column. Value will be array of values of the specified column:
max
Will return maximum value of the specific column as a single value
will return:
3
min
Will return minimum value of the specific column as a single value
will return:
1
average
Will return average value of the specific column as a single value
will return:
2
sum
Will return sum of the specific column values as a single value
will return:
2
transform
Transform function is used to transform the returning rows.
[!DANGER] Transform function should be called before returning functions.
will return
To improve performance of your application, you may want to cache results to use it multiple times. On the other hand, you may need the same result in the same code (For example: in a background job, you may have a where query in for loop) For these cases, if you use registry, then you may get rid of Cache Driver Connection
time.
Cache
You can use cache function to cache results.
Without Parameters
Example:
will cache the result after first call for 60 seconds by default.
[!TIP] Result will return result without caching if cache is disabled.
With Duration
Example:
will cache the result after first call for 120 seconds.
[!TIP] Result will return result without caching if cache is disabled.
With Duration and Cache Key
Example:
will cache the result using test-cache-key
in Cache of Default Cache Driver. Which means that, you can remove cache using following code:
With Duration, Cache Key and Cache Driver
Example:
will cache the result using test-cache-key
in Cache of apc
Driver. Which means that, you can remove cache using following code:
Registry
You can use registry to cache results for the running code.
Without Registry Key
Example:
will save the result to application registry and returned
With Registry Key
Example:
will save the result to application registry using test-key
as key. Which means that, you can access and manipulate result using following code block:
[!TIP] Although you may use registry keys while saving results to registry, there is no known use case for this feature :)
You can run insert queries using database client.
[!TIP] You can call
runInBackground
function before sending insert queries to make queries asynchronous on Elastic Search. If you want your code to wait until all queued insert tasks finish, you can usedb('elastic-search')->from(''')->refresh()
to make the code wait.
Insert
Single insert statement can be run as the following example:
will return the auto increment id of the created row.
InsertOrUpdate
Inserts or updates single record. First parameter is used to find if the row already exist,
There are two options:
If the record doesn't exist: Both parameters will be merged and inserted into database.
If the record exists: Since first parameters doesn't change, it will update the record with the values in $data array (Second Parameter)
Function returns auto increment value for the record for both cases.
Note: This function doesn't use unique indexes. It's recommended to use indexed columns for better performance.
It will check if there is a record having id => 1, if it doesnt exist, then this will become an insert statement. If it exist, then, the call become an update statement.
InsertOrIgnore
When inserting a record to database, if there is a unique index, you may get an error. When this function is used, it ignores insert errors and returns without inserting the record on error. Which means that, the record will not be inserted if it already exist.
will not insert record if there is already a record with id = 1
BulkInsert
Bulk inserts can improve performance since multiple records will be inserted in a single query.
BulkInsertOrUpdate
When bulk inserting data, if there is a unique index and new data already exists, than you may want to update the existing data. There are two options:
Updating all columns: Following query will update all columns if the records already exist.
Updating specific columns: Following query will only update the name column if the unique key already exist for the records.
BulkInsertOrIgnore
When bulk inserting data, if there is a unique index and new data already exists, than you may want to ignore the new data without any error.
You can run update queries using database client.
Update
Simple update statement can be run as the following example:
Limit
You can limit the number of rows the update query can update by using limit function.
will run the query:
Where Clause
Join Statement
Example Usage:
will run the following query:
You can run delete queries using database client.
Delete
Simple delete statement can be run as the following example:
will run the following query and will return number of rows affected
Join Statement
Multiple tables may be joined and deleted.
Will run the following query:
Note: Delete function accepts array of table names, if tables names are not given, then it will only delete the main table which is defined when table function is called. Which is
user_groups
table in the example.
When using join statements, if no delete tables is defined when delete function is called, only main table will be deleted.
Example:
Will run the following query:
You can run schema operations with Butterfly\Database
tables
Will return the list of tables in the databases.
will return the list of tables as an array list.
columns
Will return the list of columns in a table.
will return
createTable
will create a table with the specified columns.
will run the following query:
createOrUpdateTable
it checks for the table, if the table already exists, it will alter.
[!TIP] This function also checks for columns, if column information is the same, then, it will skip it, if column is not identical, it will modify the column.
[!WARNING] This function doesn't check auto_increment column for altering operations. Which means that, you may not change auto_increment column for existing tables.
will run the following query if table doesn't exist:
will run the following query if table exists, id column exists but different then current info, name column is missing.
will run the following query if table exists, id column exists and identical, name column is missing.
createColumns
will run the query
dropColumns
Drop column drops the column from table. If column doesn't exist, then it will just return true
will run the following query:
rename
You can rename tables using rename
function.
Following example renames table test
to test_2
:
will run the following query:
dropTable
Drop table removes the table from database. If table doesn't exist, then it will just return true
will run the following query:
[!TIP] Drop table function checks if table exists before running the query
Alternatively, you can also call function with table name as first parameter.
will run the following query:
[!TIP] As you may guess, if you define parameter to the function, it will be used instead of tableName property of the class.# ElasticSearch
Audience is getting more and more everyday, data you should keeps growing. User's are getting more demanding.
That brought need for different database solutions optimized for your needs. Elastic Search is one of them when you need faster search results, facets etc.
Butterfly Elastic Search implemenation aims to make developer comfortable while writing queries and maintaining code for different database technologies.
You can write down queries easier than ever using Butterfly. Just change your adapter and that's it. You can use built-in functions to handle most complex operations like Bulk Inserts, Insert Or Ignore operations, Insert Or Update operations, Fetching or updating specific columns.
The database configurations are stored in app/config/database.php
. Configurations can be customized by domain name with subfolders. Example configuration:
Database client can be reached using db()
helper. You can also reach defined databases using the database alias as the first parameter.
You can define multiple databases for different purposes. For example, you may create a logging ElasticSearch Instance / Database.
Transactions are not supported for ElasticSearch.
Since Elastic Search's main focus is searching, you can use search
function to make a generic search:
Example:
will run the query:
and search for test
in name
, surname
, email
fields.
will run the query:
and return all results as associative array.
Specifying Columns
You may want to return specific columns:
will run query:
Where
You can write where clauses in many ways. Examples are the following:
will run the query:
and return one row as associative array.
[!WARNING] Unlike MySQL Where Clauses, Elastic Search doesn't match only exact phrase when you search inside Text fields. For example: if you run
->where('name', 'John')
it will return rows where name is John or John Doe. If you want to return exact records with name: John, you should usekeyword
field type, instead oftext
.
whereIn
You can use arrays with where clauses:
will run the query:
whereNotIn
You can use arrays with where clauses:
will run the query:
whereNull
By using whereNull, you can easily filter only null values:
will run the query:
whereNotNull
will run the query:
Nested Clause
Nested SQL Queries can be generated using callback functions.
As you can see below, queries inside of the function will be evaluated seperately inside of braces and it will run:
orWhere
You can also use operators in where clauses. If you pass where clauses as an array, all clauses in the array will be joined using AND
operator.
will run:
orWhereIn
will run the query:
orWhereNull
will run the query:
orWhereNotNull
will run the query:
Distinct
Distinct query is not supported by Elastic Search Adapter.
whereBetween
will run:
boostWhere
will run:
orBoostWhere
will run:
orWhereBetween
will run:
orWhereNotBetween
will run:
whereNotBetween
will run:
Join
[!WARNING]
Join
Functions are not supported by Elastic Search.
Left Join
[!WARNING]
Join
Functions are not supported by Elastic Search.
Right Join
[!WARNING]
Join
Functions are not supported by Elastic Search.
Use Index
[!WARNING]
useIndex
is supported by Elastic Search. Will not produce an error but it will just ignore this function call.
Force Index
[!WARNING]
forceIndex
is supported by Elastic Search. Will not produce an error but it will just ignore this function call.
Order By
You can order by column ascending or descending order:
Group By
You can use group by function to get aggregations in Elastic Search Implementation
will return
For multiple columns, you can use array:
will return
Group By With Filters
You can use group by with filters function to get filtered aggregations
Group By And Size Limit
By default, there is a limit for each group by field in the Elasticsearch settings as 10. It is possible to change this limit as you can see in the following array example.
It can be used in the same way when you group by filter.
Group By Range
You can define ranges to get aggregations using Elastic Search Range Spec.
Example:
will return:
Having
[!WARNING] Having is not supported by Elastic Search. It will throw error: "Having function is not supported by ElasticSearch"
Limit
Results can be limited using limit function. Only the first number of rows will be returned.
Will run the query:
And will return first 10 rows from users
index.
Pagination
[!WARNING] Pagination doesnt work correctly in Elastic Search if no sorting is set. Please use orderBy functions before calling pagination
You can define paginate results by using paginate
function.
$limit
Sets the limit per page
20
$page_no
Sets the current page
1
Will run the following query:
And will return the results from 2nd page starting from 6.
to 10.
record.
Find
You can use find
function to return one row using identifier
will return one row with id = 1
Die
For debugging purposes, you can use die
function to output the SQL Query that will run.
will output the query that will be executed.
[!NOTE]
die
function will not run the query, just outputs it and terminates the script.
Returning Functions
Returning functions are used to execute the query and return the processed result.
Let's think of a virtual users
table with the following records:
1
John Doe
waiting
2
Jane Doe
waiting
3
Jack Hamel
active
get
Will return all rows as associative array.
will return
first
Will just return the first row as associative array
will return
count
Will return the number of rows for the query without any Group By statement.
will return:
(int) 3
one
Will return only one column value
will return the name column value of the first row.
John Doe
column
Will return the values of specific column as an array list
will return names as an array list.
keyToValue
Will return result indexed by key_column. Value will be the row as the associative array.
will return:
When you call keyToValue function with two parameters, it will use the column value
will return result indexed by key_column. Value will be value of the specified column:
keyToValues
when key_column is not unique, you can use this function to group results by key_column (e.g. status). Value will be the row as the associative array.
will return:
When key_column is not unique, you can use this function to group results by key_column (e.g. status). Value will be value of the specified column
will return result indexed by key_column. Value will be array of values of the specified column:
max
Will return maximum value of the specific column as a single value
will return:
3
min
Will return minimum value of the specific column as a single value
will return:
1
average
Will return average value of the specific column as a single value
will return:
2
transform
Transform function is used to transform the returning rows.
[!DANGER] Transform function should be called before returning functions.
will return
To improve performance of your application, you may want to cache results to use it multiple times. On the other hand, you may need the same result in the same code (For example: in a background job, you may have a where query in for loop) For these cases, if you use registry, then you may get rid of Cache Driver Connection
time.
Cache
You can use cache function to cache results.
Without Parameters
Example:
will cache the result after first call for 60 seconds by default.
[!TIP] Result will return result without caching if cache is disabled.
With Duration
Example:
will cache the result after first call for 120 seconds.
[!TIP] Result will return result without caching if cache is disabled.
With Duration and Cache Key
Example:
will cache the result using test-cache-key
in Cache. Which means that, you can remove cache using following code:
Registry
You can use registry to cache results for the running code.
Without Registry Key
Example:
will save the result to application registry and returned
With Registry Key
Example:
will save the result to application registry using test-key
as key. Which means that, you can access and manipulate result using following code block:
[!TIP] Although you may use registry keys while saving results to registry, there is no known use case for this feature :)
You can run insert queries using database client.
Insert
Single insert statement can be run as the following example:
will return the auto increment id of the created row.
InsertOrUpdate
Inserts or updates single record. First parameter is used to find if the row already exist,
There are two options:
If the record doesn't exist: Both parameters will be merged and inserted into database.
If the record exists: Since first parameters doesn't change, it will update the record with the values in $data array (Second Parameter)
Function returns auto increment value for the record for both cases.
Note: This function doesn't use unique indexes. It's recommended to use indexed columns for better performance.
It will check if there is a record having id => 1, if it doesnt exist, then this will become an insert statement. If it exist, then, the call become an update statement.
InsertOrIgnore
When inserting a record to database, if there is a unique index, you may get an error. When this function is used, it ignores insert errors and returns without inserting the record on error. Which means that, the record will not be inserted if it already exist.
will not insert record if there is already a record with id = 1
BulkInsert
Bulk inserts can improve performance since multiple records will be inserted in a single query.
BulkInsertOrUpdate
When bulk inserting data, if there is a unique index and new data already exists, than you may want to update the existing data. There are two options:
Updating all columns: Following query will update all columns if the records already exist.
Updating specific columns: Following query will only update the name column if the unique key already exist for the records.
BulkInsertOrIgnore
When bulk inserting data, if there is a unique index and new data already exists, than you may want to ignore the new data without any error.
You can run update queries using database client.
Update
Simple update statement can be run as the following example:
Limit
You can limit the number of rows the update query can update by using limit function.
will update column where id = 1
Raw Values
You can use values that are dynamic like column to column assignment, or mysql functions
will run the following SQL Query
Where Clause
You can run delete queries using database client.
Delete
Simple delete statement can be run as the following example:
will delete column where id = 5
and will return number of rows affected
will return the list of tables as an array list.
columns
Will return the list of columns in a table.
will return
createTable
will create a table with the specified columns.
[!WARNING] Work in progress
createOrUpdateTable
it checks for the table, if the table already exists, it will alter.
[!TIP] This function also checks for columns, if column information is the same, then, it will skip it, if column is not identical, it will modify the column.
[!WARNING] This function doesn't check auto_increment column for altering operations. Which means that, you may not change auto_increment column for existing tables.
[!wARNING] Column alteration checks field types not to update larger column types to smaller ones. bigint will not be converted to integer, longtext will not be converted to mediumtext or text. it first checks column type and column size.
If you need to decrease column size, you should do it manually
dropColumns
Drop column drops the column from table. If column doesn't exist, then it will just return true
[!WARNING] Work in progress
Audience is getting more and more everyday, data you should keeps growing. User's are getting more demanding.
That brought need for different database solutions optimized for your needs. MongoDB is one of them when you need Statistics Data, Big Data, more powerful Nested Objects.
Butterfly MongoDB implementation aims to make developer comfortable while writing queries and maintaining code for different database technologies.
You can write down queries easier than ever using Butterfly. Just change your adapter and that's it. You can use built-in functions to handle most complex operations like Bulk Inserts, Insert Or Ignore operations, Insert Or Update operations, Fetching or updating specific columns.
The database configurations are stored in app/config/database.php
. Configurations can be customized by domain name with subfolders. Example configuration:
Database client can be reached using db()
helper. You can also reach defined databases using the database alias as the first parameter.
[!WARNING]
mongodb
in this example is the database alias defined in configuration.
You can define multiple databases for different purposes. For example, you may create a logging MongoDB Instance / Database.
Transactions are not supported for MongoDB.
Since Elastic Search's main focus is searching, you can use search
function to make a generic search:
Example:
will run the query:
and search for test
in email
, company
fields.
will run the query:
and return all results as associative array.
Specifying Columns
You may want to return specific columns:
will run query:
Where
You can write where clauses in many ways. Examples are the following:
will run the query:
and return one row as associative array.
whereIn
You can use arrays with where clauses:
will run the query:
whereNotIn
You can use arrays with where clauses:
will run the query:
whereNull
By using whereNull, you can easily filter only null values:
will run the query:
whereNotNull
will run the query:
Nested Clause
Nested SQL Queries can be generated using callback functions.
As you can see below, queries inside of the function will be evaluated seperately inside of braces and it will run:
orWhere
You can also use operators in where clauses. If you pass where clauses as an array, all clauses in the array will be joined using AND
operator.
will run:
orWhereIn
will run the query:
orWhereNull
will run the query:
orWhereNotNull
will run the query:
Distinct
will run:
whereBetween
will run:
boostWhere
orBoostWhere
orWhereBetween
will run:
orWhereNotBetween
will run:
whereNotBetween
will run:
Join
[!WARNING]
Join
Functions are not supported by MongoDB.
Left Join
[!WARNING]
Join
Functions are not supported by MongoDB.
Right Join
[!WARNING]
Join
Functions are not supported by MongoDB.
Use Index
will run:
Group By
You can use group by function to get aggregations in Elastic Search Implementation
will return
For multiple columns, you can use array:
will return
Group By Range
You can define ranges to get aggregations using Elastic Search Range Spec.
Example:
will return:
Having
[!WARNING] Having is not supported by Elastic Search. It will throw error: "Having function is not supported by ElasticSearch"
Limit
Results can be limited using limit function. Only the first number of rows will be returned.
Will run the query:
And will return first 10 rows from users
index.
Pagination
[!WARNING] Pagination doesnt work correctly in Elastic Search if no sorting is set. Please use orderBy functions before calling pagination
You can define paginate results by using paginate
function.
$limit
Sets the limit per page
20
$page_no
Sets the current page
1
Will run the following query:
And will return the results from 2nd page starting from 6.
to 10.
record.
Find
You can use find
function to return one row using identifier
will return one row with id = 1
Die
For debugging purposes, you can use die
function to output the SQL Query that will run.
will output the query that will be executed.
[!NOTE]
die
function will not run the query, just outputs it and terminates the script.
Returning Functions
Returning functions are used to execute the query and return the processed result.
Let's think of a virtual users
table with the following records:
1
John Doe
waiting
2
Jane Doe
waiting
3
Jack Hamel
active
get
Will return all rows as associative array.
will return
first
Will just return the first row as associative array
will return
count
Will return the number of rows for the query without any Group By statement.
will return:
(int) 3
one
Will return only one column value
will return the name column value of the first row.
John Doe
column
Will return the values of specific column as an array list
will return names as an array list.
keyToValue
Will return result indexed by key_column. Value will be the row as the associative array.
will return:
When you call keyToValue function with two parameters, it will use the column value
will return result indexed by key_column. Value will be value of the specified column:
keyToValues
when key_column is not unique, you can use this function to group results by key_column (e.g. status). Value will be the row as the associative array.
will return:
When key_column is not unique, you can use this function to group results by key_column (e.g. status). Value will be value of the specified column
will return result indexed by key_column. Value will be array of values of the specified column:
max
Will return maximum value of the specific column as a single value
will return:
3
min
Will return minimum value of the specific column as a single value
will return:
1
average
Will return average value of the specific column as a single value
will return:
2
transform
Transform function is used to transform the returning rows.
[!DANGER] Transform function should be called before returning functions.
will return
To improve performance of your application, you may want to cache results to use it multiple times. On the other hand, you may need the same result in the same code (For example: in a background job, you may have a where query in for loop) For these cases, if you use registry, then you may get rid of Cache Driver Connection
time.
Cache
You can use cache function to cache results.
Without Parameters
Example:
will cache the result after first call for 60 seconds by default.
[!TIP] Result will return result without caching if cache is disabled.
With Duration
Example:
will cache the result after first call for 120 seconds.
[!TIP] Result will return result without caching if cache is disabled.
With Duration and Cache Key
Example:
will cache the result using test-cache-key
in Cache. Which means that, you can remove cache using following code:
With Duration, Cache Key and Cache Driver
Example:
will cache the result using test-cache-key
in Cache of apc
Driver. Which means that, you can remove cache using following code:
Registry
You can use registry to cache results for the running code.
Without Registry Key
Example:
will save the result to application registry and returned
With Registry Key
Example:
will save the result to application registry using test-key
as key. Which means that, you can access and manipulate result using following code block:
[!TIP] Although you may use registry keys while saving results to registry, there is no known use case for this feature :)
You can run insert queries using database client.
Insert
Single insert statement can be run as the following example:
will return the auto increment id of the created row.
Insert with raw values
Insert statements can use raw column values
will run the following query:
InsertOrUpdate
Inserts or updates single record. First parameter is used to find if the row already exist,
There are two options:
If the record doesn't exist: Both parameters will be merged and inserted into database.
If the record exists: Since first parameters doesn't change, it will update the record with the values in $data array (Second Parameter)
Function returns auto increment value for the record for both cases.
Note: This function doesn't use unique indexes. It's recommended to use indexed columns for better performance.
It will check if there is a record having id => 1, if it doesnt exist, then this will become an insert statement. If it exist, then, the call become an update statement.
InsertOrIgnore
When inserting a record to database, if there is a unique index, you may get an error. When this function is used, it ignores insert errors and returns without inserting the record on error. Which means that, the record will not be inserted if it already exist.
will not insert record if there is already a record with id = 1
BulkInsert
Bulk inserts can improve performance since multiple records will be inserted in a single query.
BulkInsertOrUpdate
When bulk inserting data, if there is a unique index and new data already exists, than you may want to update the existing data. There are two options:
Updating all columns: Following query will update all columns if the records already exist.
Updating specific columns: Following query will only update the name column if the unique key already exist for the records.
BulkInsertOrIgnore
When bulk inserting data, if there is a unique index and new data already exists, than you may want to ignore the new data without any error.
You can run update queries using database client.
Update
Simple update statement can be run as the following example:
Limit
You can limit the number of rows the update query can update by using limit function.
will update column where id = 1
Where Clause
You can run delete queries using database client.
Delete
Simple delete statement can be run as the following example:
will delete column where id = 5
and will return number of rows affected
Note: You can use all type of where clauses with update queries. Please check section for this.
Note: You can use all type of join clauses with update queries. Please check section for this.
Note: You can use all type of where clauses with update queries. Please check section for this.
Boost queries are not supported in MongoDB. Look at for boost queries.
Boost queries are not supported in MongoDB. Look at for boost queries.
Note: You can use all type of where clauses with update queries. Please check section for this.