oscarotero / simple-crud

PHP library to provide magic CRUD in MySQL/Sqlite databases with zero configuration
MIT License
242 stars 58 forks source link

Would "update" inserts empty values? #25

Closed elieobeid7 closed 6 years ago

elieobeid7 commented 6 years ago

To insert I do

$user = $db->user->create($_POST['data']);

Would that work for update too? I mean if there's an empty values in the form, would it override the existent values or would it ignore the empty values and insert what's not empty? How to update none empty values by doing the same thing as insert?

oscarotero commented 6 years ago

There's no distinction between empty values and non-empty values. If you update a row with empty values, that will be saved in the database. If you want to update a row, just select the row and update the values. And to use only the non-empty values, you can filter the array:

$user = $db->user->select()
    ->one()
    ->byId($_POST['data']['id'])
    ->run();

if ($user) {
    $data = array_filter($_POST['data']); //discard empty values
    $user->edit($data); //edit the existing user
} else {
    $user = $db->user->create($_POST['data']); //create the user
}
$user->save(); //save data
elieobeid7 commented 6 years ago

Great! what's the difference between $user->edit and $user->update ? I haven't seen that before.

Man each time I ask you a question; you show something new about this library, I wish I have the time to go through it line by line and write a complete documentation.

oscarotero commented 6 years ago

$user->edit() is a method of a row, and only edit its values (but does not save it in the database). It's the equivalent to:

foreach ($data as $field => $value) {
    $user->$field = $value;
}

$user->update() is a method of the user table, and generates a new update query (in the same way that you can generate a select, delete or insert query).

elieobeid7 commented 6 years ago

if data is related to another field, you edit, should you do

$user->relate($company);

or is relate not important in update and edit since the data exists and already related?

And one more thing I keep getting

Fatal error: Uncaught SimpleCrud\SimpleCrudException: Query 'Edit' not found in D:\xampp\htdocs\web\diaspora\common\vendor\simple-crud\simple-crud\src\QueryFactory.php:43 Stack trace: #0 D:\xampp\htdocs\web\diaspora\common\vendor\simple-crud\simple-crud\src\Table.php(111): SimpleCrud\QueryFactory->get(Object(SimpleCrud\Table), 'Edit') #1 D:\xampp\htdocs\web\diaspora\edit_user_ctrl.php(51): SimpleCrud\Table->__call('edit', Array) #2 {main} thrown in D:\xampp\htdocs\web\diaspora\common\vendor\simple-crud\simple-crud\src\QueryFactory.php on line 43

Query not found, strange

oscarotero commented 6 years ago

If the data is already related, you don't need to relate it again. $user->relate($company) only fill the user_id field with the company id.

About the exception, you're executing edit() in a table and as said in previous comment, edit() is a row function, not a table query.

//You can do this:
$user = $db->user[4];
$user->edit($newData);
$user->save();

//Or this
$db->update()
  ->data($newData)
  ->byId(4)
  ->run();
elieobeid7 commented 6 years ago

Just want to ask a small question, no need to create new issue. Is there a function that would throw an error if something goes wrong during the CRUD?

or is something like this enough?

if ($user) echo 'success';
else echo "something went wrong, please try to insert again";

The reason is I have to notify the user in case his data wasn't inserted, deleted whatever.

oscarotero commented 6 years ago

If something is wrong, an exception is throw. Isn't it enought?

elieobeid7 commented 6 years ago

Ok then I'll do it in a try catch. Thank you

elieobeid7 commented 6 years ago

I can use normal pdo exceptions? for instance to check if someone is trying to duplicates in unique field that would be

catch (Exception $e) {
    if ($e->errorInfo[1] == 1062) {
        //The INSERT query failed due to a key constraint violation.
    }
}

And where does Logger::log($message); writes the log file? The default logging path of php.ini? is it a part of the library the logger? Because it's not php's default logger.

oscarotero commented 6 years ago

I can use normal pdo exceptions? for instance to check if someone is trying to duplicates in unique field that would be

Why don't you check this before asking?

And where does Logger::log($message); writes the log file?

That is just an example of a hypothetical Logger. There's no logger provided in this package.