Download the PHP package wpmvc/database without Composer
On this page you can find all versions of the php package wpmvc/database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package database
About WpMVC Database
WpMVC Database is a robust and versatile SQL query builder designed specifically for WordPress plugins. It provides a similar experience to Laravel's Eloquent Query Builder, a well-known and widely-used PHP framework.
- About WpMVC Database
- Installation
- Create Eloquent Model
- Insert Data
- Update Data
- Delete Data
- Read Data
- Aggregates
- Retrieving Models
- All Records
- Single Record
- Select Statements
- Join
- Inner Join Clause
- Left Join / Right Join Clause
- Advanced Join Clauses
- Basic Where Clauses
- Where Clauses
- Or Where Clauses
- Advanced Where Clauses
- Where Exists Clauses
- Additional Where Clauses
- where_between / or_where_between
- where_not_between / or_where_not_between
- where_in / where_not_in / or_where_in / or_where_not_in
- Ordering, Grouping, Limit \& Offset
- Ordering
- The order_by Method
- Grouping
- The group_by \& having Methods
- Limit \& Offset
- The limit \& offset Methods
- Ordering
- Relationships
- One To One
- One To Many
- One To Many (Inverse) / Belongs To
- Constraining Query Loads
- License
Installation
To install the WpMVC Database package, simply run the following command via Composer:
Create Eloquent Model
To create an Eloquent model, you can use the following code snippet.
Insert Data
You can insert data into the posts
table using the query builder provided by Eloquent. Here's an example of how to insert a single item:
To insert multiple items at once, simply pass an array of arrays:
You can also insert an item and retrieve its ID in one step using the insert_get_id
method:
Update Data
To update a post where the post id is 100, use the following code:
Delete Data
To delete a post where the post id is 100, use the following code:
Read Data
To retrieve data, the WpMVC Database offers a variety of methods: Get all posts
Aggregates
The query builder also provides a variety of methods for retrieving aggregate values like count
, max
, min
, avg
, and sum
. You may call any of these methods after constructing your query:
Retrieving Models
All Records
To get all the posts, use the get
method as shown below:
Single Record
To retrieve a single record from the database, use the first
method as shown below:
Select Statements
You may not always want to select all columns from a database table. Using the select
method, you can specify a custom "select" clause for the query:
The distinct
method allows you to force the query to return distinct results:
Join
Inner Join Clause
To add join clauses to your SQL queries using the query builder, you can use the join
method. This method is used to perform an inner join between two or more database tables. The first argument passed to the join
method is the name of the table you want to join, and the remaining arguments specify the column constraints for the join.
In this example, we are joining the users
table with the contacts table on the id column of users
and the user_id
column of contacts
. We are also selecting all columns from users and the phone
and email
columns from contacts
.
You can even join multiple tables in a single query using the join
method multiple times. For example:
In this example, we are joining the users
table with the contacts
table and the orders
table. We are selecting all columns from users
, the phone column from contacts
, and the price column from orders
.
Here is an example of how to use the join method to join two tables:
Left Join / Right Join Clause
To perform a "left join" or "right join" instead of an "inner join", you can use the left_join
or right_join
methods respectively. These methods have the same signature as the join
method, which means that you need to pass the name of the table you want to join as the first argument, and then specify the column constraints for the join as the remaining arguments.
For example, to perform a "left join" on the users
and posts
tables using the user_id
column as the join constraint, you can do the following:
This will return all the rows from the users
table along with their matching rows from the posts
table based on the user_id
column. If a user has no matching posts, the values from the posts
table will be NULL
.
Similarly, to perform a "right join" on the users and posts
tables using the user_id
column as the join constraint, you can do the following:
This will return all the rows from the posts
table along with their matching rows from the users
table based on the user_id
column. If a post has no matching user, the values from the users
table will be NULL.
Advanced Join Clauses
You may also specify more advanced join clauses. To get started, pass a closure as the second argument to the join
method. The closure will receive a WpMVC\Database\Query\JoinClause
instance which allows you to specify constraints on the "join" clause:
If you would like to use a "where" clause on your joins, you may use the where
and or_where
methods provided by the JoinClause instance. Instead of comparing two columns, these methods will compare the column against a value:
Basic Where Clauses
Where Clauses
To get only published posts, use the where
method as shown below:
Or Where Clauses
To get only published posts or if post_title is test post, use the where
method as shown below:
Advanced Where Clauses
Where Exists Clauses
The where_exists
and where_column
methods are useful when you need to retrieve data from two different tables that have a common column.
To get all posts if the post has meta data, you can use either of the following two processes:
-
Process One: In this process, we use a closure function to define a subquery that selects
1
from thepostmeta
table where thepost_id
column inpostmeta
table is equal to theID
column in theposts
table. The closure function is passed as an argument to thewhere_exists
method to filter the posts. - Alternatively Process: In this process, we first define a variable
$post_meta
that selects1
from thepostmeta
table where thepost_id
column inpostmeta
table is equal to theID
column in theposts
table. Then we use thewhere_exists
method and pass the$post_meta
variable as an argument to filter the posts.
In both of these processes, we use the where_column
method to specify the column names in the two tables that should be compared. This allows us to filter the posts based on whether or not they have meta data.
Additional Where Clauses
where_between / or_where_between
The where_between
method verifies that a column's value is between two values:
where_not_between / or_where_not_between
The where_not_between
method verifies that a column's value lies outside of two values:
where_in / where_not_in / or_where_in / or_where_not_in
The where_in
method verifies that a given column's value is contained within the given array:
The where_not_in
method verifies that the given column's value is not contained in the given array:
Ordering, Grouping, Limit & Offset
Ordering
The order_by Method
The order_by
method allows you to sort the results of the query by a given column. The first argument accepted by the order_by
method should be the column you wish to sort by, while the second argument determines the direction of the sort and may be either asc
or desc
:
To sort by multiple columns, you may simply invoke order_by
as many times as necessary:
Grouping
The group_by & having Methods
As you might expect, the group_by
and having
methods may be used to group the query results. The having
method's signature is similar to that of the where
method:
Limit & Offset
The limit & offset Methods
You may use the limit
and offset
methods to limit the number of results returned from the query or to skip a given number of results in the query:
Relationships
Database tables are often related to one another. For example, a blog post may have many comments or an order could be related to the user who placed it. Eloquent makes managing and working with these relationships easy, and supports a variety of common relationships:
One To One
A one-to-one relationship is a very basic type of database relationship. For example, a User
model might be associated with one Phone
model. To define this relationship, we will place a Phone
method on the User
model. The Phone
method should call the has_one
method and return its result. The has_one
method is available to your model via the model's WpMVC\Database\Eloquent\Model
base class:
Eloquent assumes that the foreign key should have a value matching the primary key column of the parent. In other words, Eloquent will look for the value of the user's ID
column in the user_id
column of the Phone
record.
Now, let's retrieve all Users and their phone:
One To Many
A one-to-many relationship is used to define relationships where a single model is the parent to one or more child models. For example, a blog post may have an infinite number of meta. Like all other Eloquent relationships, one-to-many relationships are defined by defining a method on your Eloquent model:
One To Many (Inverse) / Belongs To
Now that we can access all of a post's meta, let's define a relationship to allow a meta to access its parent post. To define the inverse of a has_many relationship, define a relationship method on the child model which calls the belongs_to_one method:
Constraining Query Loads
Sometimes you may wish to a relationship but also specify additional query conditions for the relationship query. You can accomplish this by passing an array of relationships to the with method where the array key is a relationship name and the array value is a closure that adds additional constraints to the relationship query:
In with
method you can pass array for multiple relationship
License
WpMVC Database is open-sourced software licensed under the MIT license.