Several CRUD in the same PHP project

Asked

Viewed 400 times

0

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

Let’s say I have 3 CRUD to make, one would be products, the other customer and the last order.

In that case, I would have to structure the project more or less like this:

*Applying:

  • Client:CREATE READ UPDATE DELETE

  • Product:CREATE READ UPDATE DELETE

  • Request:CREATE READ UPDATE DELETE

Is there any other way, that would treat the use of fewer files or a more flexible type of structuring, without providing the use of framework?

  • I made a question related to this. I suggest that you also research about the DAO, MVC and DDD standards. Even if you do not use a framework you can base on the structure of them

  • Yes, the use of MVC will be adept to me. I wanted to know if there is any other way besides MVC. :D

1 answer

2


Assuming you don’t really want to apply a project pattern, or an elaborate framework, you can use a 'mini framework' (without irony, I couldn’t think of another kk name) for that:

Follow an example in a rudimentary way, but we can take a basis for you to apply in 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 which table, which record will be updated, and which data 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 consulted:

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

2 - Logic

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

Create In the creation, we will receive the post, separate the value 'form', which will be the table name, and run two loops, one will inform by the name of the form field the database 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 table (form), the field that will be compared and the value that must be found to update, in sequence a loop is opened to add the fields and their respective 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 receive the table (form), we will remove the form from the $_POST array, and then we will use the field key, and the value to inform what is the value to be deleted:

<?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 parameter the name of the table that must be consulted:

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

Explaining:

Since you said you didn’t want a framework, this is a very rudimentary way to have fewer files, this is a pattern you can use to submit multiple forms, using a logical basis, that will handle the 'Names' of your form fields and build your queries based on them, if you keep this simple pattern you can even work with large forms.

However, it is not a good practice, as you would often have to process the data and work with aliases (one of them is changing the field names after the pro user submission not knowing 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 interaction functions with database.

Finalizing

One of the most widely used and relatively easy to apply patterns is the MVC, where you will have a Ccontroller responsible for knowing what needs to be called, Models to work with the logical part of treatment and submission, and a View to return the answers.

A great example of CRUD using the MVC, is the way the routes of the Laravel, who call a controller and this controller informs the model what she should do or bring some View in response.

As you said in the question that you didn’t want a framework, I’m not going to delve into this pattern, it’s an explanation of an alternative way of having fewer files to work with multiple CRUD’s, but personally, I believe that using the pattern of some framework makes work easier and faster.

  • That, I’m aware of using a standard MVC. Which I need to do just like that, in fact basically pure PHP. But I will follow your tips, will help me a lot! Thanks :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.