Several CRUDs in the same PHP project

0

Hello, I have a question about structuring a project, in case it is in PHP.

Let's say that I have 3 CRUDs to do, one would be a product, another a customer, and the last one to order.

In this case, I would have to structure the project something like this:

* Application:

  • Client: CREATE        READ        UPDATE        DELETE

  • Product: CREATE        READ        UPDATE        DELETE

  • Order: CREATE       READ       UPDATE       DELETE

Is there any other way, which would address the use of fewer files or a more flexible structuring type, without endowing the framework?

    
asked by anonymous 03.04.2018 / 01:07

1 answer

1

Assuming you do not really want to apply a design pattern, or an elaborate framework, you can use a 'mini framework' (without irony, I could not think of another kk name):

Here's an example of a rudimentary way, but one that you can use to build your project:

1 - Forms

Create

For creation, we will have to inform a table, and the fields that will be in this table:

<form action="create.php" method="post">
  <input type="hidden" name="form" value="clientes">
  <input type="text" name="nome">
  <input type="text" name="email">
  <input type="submit" value="Salvar usuário">
</form>

Update

To update we will inform you which table, which registry will be updated, and the data that will update:

<form action="update.php" method="post">
  <input type="hidden" name="form" value="clientes">
  <input type="hidden" name="update" value="cliente 1">
  <input type="hidden" name="field" value="nome">
  <input type="text" name="nome">
  <input type="text" name="email">
  <input type="submit" value="Editar usuário">
</form>

Delete

To delete, we only need to inform the table, and the record that will be deleted:

<form action="delete.php" method="post">
  <input type="hidden" name="form" value="clientes">
  <input type="text" name="nome" value="cliente 1">
  <input type="submit" value="Excluir usuário">
</form>

Read

To read the data, we will include a file with the function, and as a function parameter, we inform the table that will be queried:

<?php
include 'read.php';
echo read('clientes');

2 - Logic

Now let's go to the logical part, they are simple files that will treat the submitted data via $_POST to construct the queries:

Create In the creation, we will receive the post, separate the 'form' value, which will be the name of the table, and rotate two loops, one will inform the field name of the form the bank field, and another loop will inform the respective values:

<?php
$sql = 'INSERT INTO ' . $_POST['form'] . ' (';
unset($_POST['form']);
$c = 1;
foreach ($_POST as $key => $value) {
  if(count($_POST) > $c){
    $sql .= $key.',';
  }else{
    $sql .= $key;
  }
  $c+=1;
}
$sql .= ') VALUES (';

$c = 1;
foreach ($_POST as $key => $value) {
  if(count($_POST) > $c){
    $sql .= '"'.$value.'"'.',';
  }else{
    $sql .= '"'.$value.'"';
  }
  $c+=1;
}
$sql .= ')';
echo $sql;

Update

To update the data, we receive the form (form), the field that will be compared and the value that must be found to update, in the sequence a loop is opened to add the fields and their values:

<?php
$sql = 'UPDATE ' . $_POST['form'] . ' SET ';
unset($_POST['form']);
$update =$_POST['update'];
unset($_POST['update']);
$field = $_POST['field'];
unset($_POST['field']);
$c = 1;
foreach ($_POST as $key => $value) {
  if(count($_POST) > $c){
    $sql .= $key.'="'.$value.'",';
  }else{
    $sql .= $key.'="'.$value.'"';
  }
  $c+=1;
}
$sql .= ' WHERE ' .$field . ' = "' . $update . '"';

echo $sql;

Delete

To delete the value, we will also get the table (form), we will remove the form from the $ _POST array, and in the sequence we will use the field key, and the value to inform which value to delete: p>

<?php
$sql = 'DELETE FROM ' . $_POST['form'] . ' WHERE ';
unset($_POST['form']);

$sql .= key($_POST) . ' = "' . $_POST[key($_POST)] . '"';

echo $sql;

Read

To read the data, we will have a function that will receive as a parameter the name of the table to be queried:

<?php
function read($tabela){
  $sql = 'SELECT * FROM ' . $tabela;
  return $sql;
}

Explaining:

As I said I did not want a framework, this is a very rudimentary way to have fewer files, this is a standard that you can use to submit multiple forms, using a logical base, that will handle the names of the fields. your form and build your queries based on them, keep that simple pattern you can work up to with large forms.

However, this is not a good practice because you would have to deal with data often and work with aliases (one is to change the field names after user submission does not know the real names of the fields in your database.)

There are more organized patterns where you can receive each form in a separate class, and unify the functions of interacting with the bank.

Finishing

One of the most commonly used and relatively easy-to-apply standards is the standard MVC where you will have a C controller responsible for knowing what needs to be called, M odels to work with the logical part of handling and submission, and a V iew to return the answers.

A great example of CRUD that uses MVC is the way the routes work Laravel , which call a controller . > and this driver tells the model what it should do or bring some View as an answer.

As you said in the question that I did not want a framework, I will not delve into this pattern, it is an explanation of an alternative way of having fewer files to work with several CRUDs, but personally, I believe using the framework standard facilitates and greatly streamlines the work.

    
03.04.2018 / 02:09