<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: RUHAMYA Elie</title>
    <description>The latest articles on DEV Community by RUHAMYA Elie (@elieru).</description>
    <link>https://dev.to/elieru</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2504425%2Fda2425b9-96be-4229-abad-3528a33eb2da.jpeg</url>
      <title>DEV Community: RUHAMYA Elie</title>
      <link>https://dev.to/elieru</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elieru"/>
    <language>en</language>
    <item>
      <title>Easy CRUD and database connection with OpenCrud</title>
      <dc:creator>RUHAMYA Elie</dc:creator>
      <pubDate>Sun, 22 Dec 2024 08:59:42 +0000</pubDate>
      <link>https://dev.to/elieru/easy-crud-and-database-connection-with-opencrud-2m1n</link>
      <guid>https://dev.to/elieru/easy-crud-and-database-connection-with-opencrud-2m1n</guid>
      <description>&lt;h2&gt;
  
  
  🌐 Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OpenCrud&lt;/strong&gt; 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.&lt;/p&gt;

&lt;p&gt;Some challenges :&lt;/p&gt;

&lt;p&gt;📌 How to connect my app on MySQL database?&lt;br&gt;
⚙️ How to make CRUD easier to use?&lt;/p&gt;

&lt;p&gt;In this documentation, we want to explain you how OpenCRUD package can be connected to your application and use different operations to manage your datas from a mySql database.&lt;/p&gt;

&lt;h2&gt;
  
  
  🛠️ Installation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Be sure to have a PHP version that is up to date.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You might know object-oriented programming, or you might not.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Connection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Download and insert the package in your project.&lt;br&gt;
&lt;a href="https://github.com/ElieRu/OpenCRUD/archive/refs/heads/main.zip" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create the mySQL database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initialise your database informations in the &lt;code&gt;/database/db-infos.php&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Link the &lt;code&gt;init.php&lt;/code&gt; in the file that you need to instantiate &lt;code&gt;OpenCRUD&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;By default, initialise the &lt;code&gt;$dbname&lt;/code&gt; variable with your database's name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your table's attributes will have the &lt;code&gt;null&lt;/code&gt; value by default. Except the primary key.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Instantiation
&lt;/h3&gt;

&lt;p&gt;To interact with OpenCrud's database, create an object instance. This object acts as a representation of one or more database tables, and it provides methods for performing CRUD operations (Create, Read, Update, and Delete) on the table data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# I instantiate the users table&lt;br&gt;
$users = new Crud($users, $attributes, $type);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The params required :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;array $tables&lt;/code&gt; is specify the table(s) that we need to use.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;array $attributes&lt;/code&gt; will have access to the table's attributes and to the foreigner key(s) also if necessary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;bool $type&lt;/code&gt; return a row of values in case of false value. Else, it returns a row of objects values.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🚀 Getting started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  I. Create
&lt;/h3&gt;

&lt;p&gt;The create operation allow you to save datas by calling the create method of the instantiated object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# The save a data&lt;br&gt;
$users-&amp;gt;create($formData);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The create method requires an array of data to be saved, excluding the primary key. However, it accepts foreign keys for establishing relationships with other data entities.&lt;/p&gt;

&lt;h3&gt;
  
  
  II. Read
&lt;/h3&gt;

&lt;p&gt;The read operation retrieves datas from the database and display them to the user interface. The read operation can handle different parameters as needed. To improve code organization and readability, it's recommended to assign the read method to a variable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# I get datas&lt;br&gt;
$getDatas = $users-&amp;gt;read($fields, $condition, $values, $limit);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The required params :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;array|null $fields&lt;/code&gt; select attribute(s) of the table instantiated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sort datas with the &lt;code&gt;string|null $condition&lt;/code&gt; if necessary.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;# Query statement&lt;br&gt;
$condition="name='john' and postname='doe'"&lt;br&gt;
=&amp;gt; The $values param should have null value&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Prepared statement&lt;br&gt;
$condition="name=? and postname=?"&lt;br&gt;
=&amp;gt; The $values param should be an array.&lt;br&gt;
ex. $values = ['john', 'doe'];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note : The fields param can be initialize by '*' to return all datas.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;array $values&lt;/code&gt; must get values of the attributes in the condition. &lt;/p&gt;

&lt;p&gt;Note : The initialisation of this variable require a prepared statement.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;array|int $limit&lt;/code&gt; select datas. Else, you can use an array.&lt;br&gt;
&lt;code&gt;# Select only 5 datas&lt;br&gt;
$limit = 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Select by the 5th to 10th data&lt;br&gt;
$limit = [5, 10];&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  III. Update
&lt;/h3&gt;

&lt;p&gt;The update operation refers to actions that modify existing data within a database or storage system. It allows you to change specific values or attributes of a record without deleting and recreating it entirely.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Update users data&lt;br&gt;
$users-&amp;gt;update($id, $formData);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The required params :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int $id&lt;/code&gt; select the specific data to update.&lt;br&gt;
&lt;code&gt;array $formDatas&lt;/code&gt; contain new informations of a data.&lt;/p&gt;

&lt;p&gt;Note : The &lt;code&gt;array $formData&lt;/code&gt; should have the values of attributes of the table.&lt;/p&gt;

&lt;h3&gt;
  
  
  IV. Delete
&lt;/h3&gt;

&lt;p&gt;The Delete operation refers to the functionality that allows you to remove data from a database or storage system. It's the process of permanently erasing a specific record or set of records based on certain criteria.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Remove a user data&lt;br&gt;
$users-&amp;gt;delete($id);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The required param :&lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;int $id&lt;/code&gt; param, you can select the data.&lt;/p&gt;

&lt;h2&gt;
  
  
  📌 Conclusion
&lt;/h2&gt;

&lt;p&gt;OpenCrud is a powerful tool for backend developers using native PHP. It simplifies connecting to MySQL databases and managing CRUD operations. However, this open-source project needs updates to improve performance and code maintainability. Without these improvements, its utility is limited to smaller projects.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Contribute (Repository Link)&lt;br&gt;
&lt;a href="https://github.com/ElieRu/open-crud" rel="noopener noreferrer"&gt;https://github.com/ElieRu/open-crud&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Link&lt;br&gt;
&lt;a href="https://open-crud.netlify.app/" rel="noopener noreferrer"&gt;https://open-crud.netlify.app/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Linkdin&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/elie-ruhamya-996826285/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/elie-ruhamya-996826285/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
