Easy CRUD and database connection with openCrud
December 22, 2024

Easy CRUD and database connection with openCrud


introduce

open crude oil is an open source package created in native PHP that simplifies connecting to MySQL databases and makes it easier to manage databases using object-oriented programming.

Some challenges:

📌 How do I connect my application to a MySQL database?
⚙️How to make CRUD easier to use?

In this document we would like to explain to you how Open addition, deletion, modification and search The suite can connect to your application and use different operations to manage data in the mySql database.


🛠️Installation


Require

  • Make sure you have the latest PHP version.

  • You may or may not know something about object-oriented programming.


connect

Note: By default, initialization $dbname Variable with your database name.

MySql Rules: Your table’s properties will have null Default value.Except primary key.


Instantiate

To interact with OpenCrud’s database, create an object instance. This object serves as a representation of one or more database tables and provides methods for performing CRUD operations (create, read, update, and delete) on table data.

# I instantiate the users table
$users = new Crud($users, $attributes, $type);

Required parameters:

  • array $tables Specify the table we need to use.

  • array $attributes If necessary, you can also access table attributes and foreign keys.

  • bool $type If the value is false, a row of values ​​is returned. Otherwise, it returns a row of object values.


Get started 🚀


create

The create operation allows you to save data by calling the create method of an instantiated object.

# The save a data
$users->create($formData);

The create method needs to save an array of data, excluding the primary key. However, it accepts foreign keys to establish relationships with other data entities.


read

Read operations retrieve data from the database and display it to the user interface. Read operations can handle different parameters as needed. In order to improve the organization and readability of the program code, it is recommended to assign the read method to variables.

# I get datas
$getDatas = $users->read($fields, $condition, $values, $limit);

Required parameters:

  • array|null $fields Select the properties of the materialized table.
  • Sort data string|null $condition if needed.

# Query statement
$condition="name="john" and postname="doe""
=> The $values param should have null value

# Prepared statement
$condition="name=? and postname=?"
=> The $values param should be an array.
ex. $values = ['john', 'doe'];

Note: Field parameters can be initialized with “*” to return all data.

array $values The value of the attribute in the condition must be obtained.

Note: Initialization of this variable requires a prepared statement.

array|int $limit Select data. Otherwise, you can use an array.

$limit = 5

# Select by the 5th to 10th data
$limit = [5, 10];


renew

Update operations refer to operations that modify existing data in the database or storage system. It allows you to change specific values ​​or attributes of a record without completely deleting and re-creating it.

# Update users data
$users->update($id, $formData);

Required parameters:

int $id Select the specific data to update.
array $formDatas New information containing data.

notes : this array $formData Should have the table’s attribute values.


delete

Delete operations are functions that allow you to delete data from a database or storage system. This is the process of permanently deleting a specific record or group of records based on specific criteria.

# Remove a user data
$users->delete($id);

Required parameters:

along with int $id param, you can select the data.

2024-12-22 08:59:42

Leave a Reply

Your email address will not be published. Required fields are marked *